private bool grounded = true; public float jumpPower = 190; public float Power = 10f; private bool hasJumped = false; public Rigidbody rb; // Use this for initialization void Start () { rb = GetComponent<Rigidbody>(); } // Update is called once per frame void Update () { // Do something if(!grounded && rb.velocity.y == 0) { grounded = true; } if (Input.GetKey (KeyCode.W) && grounded == true) { hasJumped = true; } if (Input.GetKey(KeyCode.D)) { transform.Translate(Vector3.right * Power * Time.deltaTime, Space.World); } if (Input.GetKey(KeyCode.A)) { transform.Translate(Vector3.right * -Power * Time.deltaTime, Space.World); } } void FixedUpdate (){ if(hasJumped){ rb.AddForce (transform.up*jumpPower); grounded = false; hasJumped = false; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter