首页 文章

Unity3d charactermotor通过/滑动移动平台

提问于
浏览
0

目前在我的游戏中实现移动平台,我已经设法使用开关和案例来获得平台但是当涉及将玩家保持在平台上时,平台在玩家下滑动并且玩家在平台通过之后或在平台上升的情况下,玩家通过平台坠落

我知道unity是一个默认的charactermotor,它包含了javascript中的脚本,但是我想制作一个更简单的charactermotor,它不像unity提供的那样复杂 .

我尝试将移动平台脚本中的父母代码(例如下面的1)添加到无效:

void triggerOnStay(Collider collider)
{
    if (collider.tag == "Player")
    {
        collider.transform.parent = transform.parent;
    }
}

void triggerOnLeave(Collider collider)
{
    if (collider.tag == "Player")
    {
        collider.transform.parent = null;
    }
}

想知道你是否有任何想法,我可以添加或做什么,以允许我的charactermotor能够留在移动平台,无论他们是从左到右还是向上和向下 .

Charactermotor:

public class CharacterMotor2 : MonoBehaviour {
//Variables
public Transform respawnPoint1;
public Transform respawnPoint2;
public Transform respawnPoint3;


public Transform teleporterPoint1;
public Transform teleporterPoint2;

//public Transform nextLevel;
public float speed;
public float jumpSpeed; 
public float gravity;
public Vector3 moveDirection = Vector3.zero;
public float downwardForce;
public float terminalVelocity;
public float leftForce;
public float forwardForce;
public float airSpeed;

void Update() 
{
    CharacterController controller = GetComponent<CharacterController>();
    // is the controller on the ground?
    if (controller.isGrounded) 
    {
        //Feed moveDirection with input.
        moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
        moveDirection = transform.TransformDirection (moveDirection);
        downwardForce = 0;
        //Multiply it by speed.
        moveDirection *= speed;

        //Jumping
        if (Input.GetButton ("Jump"))
        {
            downwardForce -= jumpSpeed;
        }   
    } 
    else 
    {
        if (downwardForce < terminalVelocity)
        {
                moveDirection *= airSpeed;
                downwardForce += gravity * Time.deltaTime;
        }
    }
    //Applying gravity to the controller
    moveDirection.y -= downwardForce * Time.deltaTime;
    //Making the character move
    controller.Move(moveDirection * Time.deltaTime);
}
void OnTriggerEnter(Collider collider)
{           
    if (collider.tag == "smallTrampoline") 
    {
        downwardForce *= -1 * collider.gameObject.transform.GetComponent<SmallTrampolineBounce> ().jumpSpeed;

    }
    if (collider.tag == "largeTrampoline") 
    {
        downwardForce *= -2 * collider.gameObject.transform.GetComponent<SmallTrampolineBounce> ().jumpSpeed;
    }
    if (collider.tag == "leftTrampoline")   
    {
        downwardForce *= -1 *collider.gameObject.transform.GetComponent<SmallTrampolineBounce> ().jumpSpeed;
    }
    if (collider.tag == "rightTrampoline")  
    {
        downwardForce *= -1 *collider.gameObject.transform.GetComponent<SmallTrampolineBounce> ().jumpSpeed;    
    }
    if (collider.tag == "forwardTrampoline")    
    {
        downwardForce *= -1 *collider.gameObject.transform.GetComponent<SmallTrampolineBounce> ().jumpSpeed;    
    }
    if (collider.tag == "backwardTrampoline")   
    {
        downwardForce *= -1 *collider.gameObject.transform.GetComponent<SmallTrampolineBounce> ().jumpSpeed;    
    }
    if (collider.tag == "Respawn") 
    {
        transform.position = respawnPoint1.position;
    }
    if (collider.tag == "NextRespawn")
    {
        transform.position = respawnPoint2.position;
    }
    if (collider.tag == "FinalRespawn")
    {
        transform.position = respawnPoint3.position;
    }

    if (collider.tag == "Teleporter") 
    {
        transform.position = teleporterPoint1.position;
    }
    if (collider.tag == "2ndTeleporter")
    {
        transform.position = teleporterPoint2.position;
    }






    if (collider.tag == "NextLevel") 
    {
        Application.LoadLevel(1);

    }

1 回答

  • 0

    一旦玩家在平台上并接地,您应该尝试将平台的X,Y和Z速度添加到播放器,而不是将角色养育到移动平台 . 如果玩家在平台上静止不动,则玩家的速度将与平台相匹配,并且玩家将与平台一起移动,就像它是一个子对象一样 . 这也允许玩家能够在平台上自由移动 .

    查看此答案以获得更深入的解决方案:http://answers.unity3d.com/questions/8207/charactercontroller-falls-through-or-slips-off-mov.html

相关问题