local jumping and diagonal movement

I am building my first 3D game right now. As movement is almost working fine now, there are still two things that I can’t figure out how to improve.

  1. As I hit “W” and “D” at the same time my Rigidbody should move diagonal. But instead it’s moving only in the direction I hit last.
  2. I want my rigidbody to jump in the direction it is facing after turning left (or right). Instead it’s always jumping in the same global direction.

This is my code:

public class PlayerMovement : MonoBehaviour {
	public float moveSpeed;
	public float jumpPower;
	public float turningSpeed;
	public Rigidbody rb;
	public Transform groundCheck;
	public float groundRadius;
	public LayerMask whatIsGround;

	void Start ()
		rb = GetComponent<Rigidbody> ();


	void FixedUpdate ()
		if (Physics.CheckSphere (groundCheck.position, groundRadius, whatIsGround)) {							
				rb.MovePosition(transform.position + transform.forward * moveSpeed * Time.deltaTime);
				rb.MovePosition(transform.position - transform.forward * moveSpeed * Time.deltaTime);
				rb.MovePosition(transform.position + transform.right * moveSpeed * Time.deltaTime);
				rb.MovePosition(transform.position - transform.right * moveSpeed * Time.deltaTime);
			if (Input.GetKeyDown (KeyCode.Space)) {																
				rb.AddForce (Vector3.up * jumpPower, ForceMode.Impulse);
				rb.AddForce (Vector3.forward * jumpPower, ForceMode.Impulse);
			transform.Rotate(Vector3.up * turningSpeed * Time.deltaTime);
			transform.Rotate(Vector3.down * turningSpeed * Time.deltaTime);

I would appreciate some help :slight_smile:

Unfortunately I am also struggling with this right now. Although I have found a solution, it does mess with the physics a bit and is annoying scene as I am doing a top-down 2D game, but it should work just fine with you…

Basically the only way to fix your issue of moving diagonally and possibly jumping is by, instead of using addforce, use a different method like rigidbody.transform.position or rigidbody.velocity.

I hope this helped you out in some way because it is annoying me too :slight_smile: