Unity3d charactermotor通过/滑动移动平台

时间:2014-03-20 02:28:28

标签: c# unity3d

目前在我的游戏中实施移动平台,我设法使用交换机& amp;然而,当涉及到将玩家保持在平台上时,平台在玩家下滑动并且玩家在平台通过后掉落或者在平台上升的情况下玩家通过平台坠落

我知道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:

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 :(得分:0)

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

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