I need help with the movement script that i use

I am very new to coding and i have problems with the movement and camera script im using.
I am using a character controller.
the problem is that if i look down and move with s (backwards) i begin to jump.
For some reason it registers that as jumping and i dont know how to fix it. can anyone help me?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour {
     public float speed = 25.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;

    
     private float turner;
     private float looker;
     public float sensitivity = 5;
    
     // Use this for initialization
     void Start () {
        
     }
    
     // Update is called once per frame
     void Update () {
         CharacterController controller = GetComponent<CharacterController>();
         // is the controller on the ground?
         if (controller.isGrounded) {
             //Feed moveDirection with input.
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             //Multiply it by speed.
             moveDirection *= speed;
             //Jumping
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
            
         }
         turner = Input.GetAxis ("Mouse X")* sensitivity;
         looker = -Input.GetAxis ("Mouse Y")* sensitivity;
         if(turner != 0){
             //Code for action on mouse moving right
             transform.eulerAngles += new Vector3 (0,turner,0);
         }
         if(looker != 0){
             //Code for action on mouse moving right
             transform.eulerAngles += new Vector3 (looker,0,0);
         }
         //Applying gravity to the controller
         moveDirection.y -= gravity * Time.deltaTime;
         //Making the character move
         controller.Move(moveDirection * Time.deltaTime);
     }
}

I believe your problem is line 29.

What that does is transform the inputs (tilting them) in both up / down and left/right (heading).

If you look down, then “BACK” becomes straight back up into the air behind you.

You probably ONLY want to change those inputs according to compass heading, so that “W” is always “the way the camera is facing.” Otherwise you want the inputs to stay flat.

Assuming you don’t look up or down past 90 degrees (this would cause gimbal lock), you might get away with changing line 29 above to instead be:

// make a rotation but only around our vertical axis.
Quaternion heading = Quaternion.Euler( 0, transform.eulerAngles.y, 0);
// bend our inputs around that vertical axis so they are relative to our facing
moveDirection = heading * moveDirection;

Just for reference, here is another character mover based on the Unity original example code, which had issues.

https://discussions.unity.com/t/811250/2

Ah thank you very much! it worked flawlessly.
But i have another problem, When i look down enough i will glitch the camera into thinking that it is looking down but upside down. so like going past the camera lock? do you also now how to fix that?

The way you add to .eulerAngles on line 41 and 45 is going to potentially cause gimbal lock.

You may be able to mitigate it by reading out, adding looker to it, then clamping and putting it back, but most likely you will still encounter gimbal lock problems with this approach.

Unfortunately you need to be able to reason about a mouse look script and the angles involved before you can make one yourself. If you just google for it there are like ten billion examples, and probably half of them have this same bug. I’m not exaggerating. Try maybe doing some tutorials from Brackeys to get a good mouselook going.