Unity3d 人物的跳躍

zsdeus133發表於2018-03-16

果然谷歌就是無敵, 不過都是定位到unity的community裡的

void Update(){
        // Jump
        if (Input.GetButtonDown("Jump"))
        {
            if (grounded == true)
            {
                float h = Input.GetAxis("Horizontal"); // A D
                float v = Input.GetAxis("Vertical");   // W S

                //Debug.Log(h + " " + v);
                if (Input.GetKey("w"))
                {
                    playerRigidbody.velocity = transform.forward * (v * speed);
                }
                if (Input.GetKey("s"))
                {
                    playerRigidbody.velocity = transform.forward * (v * speed);
                }
                if (Input.GetKey("a"))
                {
                    playerRigidbody.velocity = transform.right * (h * speed);
                }
                if (Input.GetKey("d"))
                {
                    playerRigidbody.velocity = transform.right * (h * speed);
                }
                playerRigidbody.AddForce(new Vector3(0f, jumpForce, 0f));
                grounded = false;
            }
        }
}

相關文章