I’ve been tinkering with the code, but I can’t find anything that works. I’m making a 3D game, and writing in C#. Here’s the code that I have now.
public class PlayerController : MonoBehaviour {
public float speed;
public float jumpHeight;
private Rigidbody rb;
void Start ()
{
//Got too lazy, so added this in.
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
//Movement on the X and Z axes
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
// The Jump Code
if (Input.GetKeyDown (KeyCode.Space))
{
Vector3 jump =new Vector3 (moveHorizontal, jumpHeight, moveVertical);
rb.AddForce (jump);
}
}
}
Now what? I want to do this in the shortest and the most efficient way possible, without using too many variables.