I am trying to make a very simple 3D plat-former with side to side movement and jumping; however, nothing seems to work when it comes to jumping. This code would work just fine if it weren’t for the, roughly, one-in-three chance of launching way to high in the air. I searched ideas for jump scripts and saw that other people have had this same problem but I never saw a solution. Can anyone help?
using UnityEngine;
using System.Collections;
public class playerMove : MonoBehaviour {
public float jump;
public float speed;
Rigidbody rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
if (Input.GetKey("right"))
{
rb.AddForce(speed * Time.deltaTime, 0, 0);
}
if (Input.GetKey("left"))
{
rb.AddForce(-speed * Time.deltaTime, 0, 0);
}
if (Input.GetKeyDown("up"))
{
rb.AddForce(0, jump, 0);
}
}
}
Why not? Does step have a value? Are you assigning the correct rigidbody? Is your rigidbody kinematic? It’s a very simple application of code that does work. I use it frequently.
Youre welcome, by the way you should keep your AddForce in FixedUpdate as thats the place for all physics stuff. Best to set a flag in Update to tell FixedUpdate to jump, then reset that flag in FixedUpdate after it has done the jump.