Need help on adding gravity to my player movement

I don’t really know how to add proper gravity and jumping to my Player

My Update method is at the bottom of this post. moveLeftRight and moveForwardBack are simply the magnitudes of the movement. They are dependent on WASD. The Move function is basically this magnitude given from WASD and multiplied to the forward and right vectors of the Player transform. My player transform correctly aligns it’s forward (z-axis) vector with with I point my mouse

I just have two issues.

The first issue is that when my mouse looks up, then the transform.forward vector gains a y component. If I hold W, there is a “fight” between the negative y and the positive forward vector’s y component. If gravity isn’t strong enough, then my Player will start flying. Similar effect when I look down and hold D

My second issue is that my jump isn’t smooth, rather my Player gets “teleported” to the jump height

Thank you

void Update()
    Vector3 move;
    float moveLeftRight = Input.GetAxis("Horizontal")*Time.deltaTime*movementSpeed;
    float moveForwardBack = Input.GetAxis("Vertical")*Time.deltaTime*movementSpeed;
    //new Vector3(0,-2,0) is simulating gravity by keeping the character controller to the ground
    move = transform.right*moveLeftRight + transform.forward*moveForwardBack + new Vector3(0,-2,0);
1 Like

If the problem is with the non-zero y component of transform.forward, then you could zero this out before using it in the move vector.

Vector3 forwardDirection= transform.forward;
forwardDirection.y = 0;

Vector3 righDirection = transform.right;
rightDirection.y = 0;

move = rightDirection*moveLeftRight + forwardDirection*moveForwardBack;