如何让游戏对象向前跳?

时间:2015-08-02 19:12:59

标签: c# unity3d gameobject

嗨,我真的很擅长这一点,想要一些帮助,我想知道如何让我的游戏对象向前跳跃(你越往握它越久跳跃),我有我的游戏对象上有一个僵硬的身体,以防万一。

public class PlayerScript : MonoBehaviour {

public float speed;

private Vector3 dir;

// Use this for initialization
void Start () 
{
    dir = Vector3.zero;
}

// Update is called once per frame
void Update () 
{
    if (Input.GetMouseButtonDown(0)) 
    {
        if (dir == Vector3.forward)
        {
            dir = Vector3.right;
        }
        else 
        {
            dir = Vector3.forward;
        }

    }

    float AmToMove = speed * Time.deltaTime;
    transform.Translate (dir * AmToMove);
}

到目前为止,我只是设法让它向前和向右移动,但我希望它向前跳并向右跳。

1 个答案:

答案 0 :(得分:2)

public float thrust;
        public Rigidbody rb;
        void Start() {
            rb = GetComponent<Rigidbody>();
        }
        void FixedUpdate() {
            rb.AddForce(transform.up * thrust);
        }
//This will add a force to the rigidbody upwards. 

如果你想使用刚体进行移动,你应该对它施加一个力。现在你只是移动对象的世界变换。

相关问题