this is my script, just add it on to a cube (that has rigidbody on it), its on c# by the way- using UnityEngine; using System.Collections; public class MovingPlayer : MonoBehaviour { public float moveSpeed = 5f; public float turnSpeed = 150f; public int jumpHeight = 5; void Update () { if(Input.GetKey(KeyCode.W)) transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.S)) transform.Translate (Vector3.forward * -moveSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.A)) transform.Rotate (Vector3.up * -turnSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.D)) transform.Rotate (Vector3.up * turnSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.Space)) transform.Translate (Vector3.up * jumpHeight * Time.deltaTime); } }
If your cube has a rigidbody on it, try applying it a force instead of moving the transform. This way you'll have the physics engine doing the calculations for you. If you don't have/want a rigidbody on your cube, then you have to do the calculation yourself. Jump happens over time, with one press and not holding (usually at least) so when the user presses the jump button, you do not translate it but instead you could initialize a timer and for example you translate up for half the timer and translate down for half the other (that's just a hint as to how to do it, you'll figure out what best fits your needs if you don't want to do it with the rigidbody...)