Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Character Controller jumping and moving right and left not working.

Discussion in 'Scripting' started by BluesyPompanno, Sep 24, 2017.

  1. BluesyPompanno

    BluesyPompanno

    Joined:
    Jan 1, 2017
    Posts:
    37
    Character movement not moving character to right or left.
    Hello i have a question. I am trying to write my own character movement system.

    The player can walk and jump.

    The problem is when the player pushed the D or A or S button he moves forward ( he is supposed to move forwards after the W button is pressed or when when moving diagonaly).How can i repair this ?

    This is my script.
    Code (CSharp):
    1.  
    2. public float walkSpeed = 10f; public Camera cameraPlayer;
    3.  
    4.     private float mouseSensitivity = 2.0f;
    5.     public Vector2 LookRange = new Vector2(-60f, 60f);
    6.     float mouseX;
    7.     float mouseY;
    8.     CharacterController charController;
    9.     public float gravity = -12f;
    10.     public float jumpHeight = 1f;
    11.     float velocityY;
    12.     float currentSpeed;
    13.     public float turnSmoothTime = 0.2f;
    14.     float turnSmoothVelocity;
    15.     public float speedSmoothTime = 0.1f;
    16.     float speedSmoothVelocity;
    17.     private void Start()
    18.     {
    19.          charController = GetComponent<CharacterController>();
    20.     }
    21.     private void Update()
    22.     {
    23.          Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
    24.          Vector3 directionInput = input.normalized;
    25.            
    26.          //transform.Translate(moveAmount);
    27.          transform.Rotate(0, mouseX, 0);
    28.          mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
    29.          mouseY -= Input.GetAxis("Mouse Y") * mouseSensitivity;
    30.          mouseY = Mathf.Clamp(mouseY, LookRange.x, LookRange.y);
    31.          cameraPlayer.transform.localRotation = Quaternion.Euler(mouseY, 0, 0);
    32.          if (Input.GetKeyDown(KeyCode.Space))
    33.          {
    34.              Jump();
    35.          }
    36.          MoveThePlayer(directionInput);
    37.     }
    38.     void MoveThePlayer(Vector3 directionInput)
    39.     {
    40.          Vector3 velocity = directionInput * walkSpeed;
    41.          Vector3 moveAmount = velocity * Time.deltaTime;
    42.          moveAmount = transform.TransformDirection(moveAmount);
    43.          float targetSpeed = walkSpeed * directionInput.magnitude;
    44.          currentSpeed = Mathf.SmoothDamp(currentSpeed,targetSpeed,ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
    45.          Vector3 velocityMovement = transform.forward * currentSpeed + Vector3.up * velocityY;
    46.        
    47.          charController.Move(velocityMovement * Time.deltaTime);
    48.          velocityY += Time.deltaTime * gravity;
    49.          currentSpeed = new Vector3(charController.velocity.x,0, charController.velocity.z).magnitude;
    50.          if (charController.isGrounded)
    51.          {
    52.              velocityY = 0;
    53.          }
    54.     }
    55.     void Jump()
    56.     {
    57.          if (charController.isGrounded)
    58.          {
    59.              float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
    60.              velocityY = jumpVelocity;
    61.          }
    62.     }
    63.     float GetModifiedSmoothTime(float smoothTime)
    64.     {
    65.          if (charController.isGrounded)
    66.          {
    67.              return smoothTime;
    68.          }
    69.          return smoothTime;
    70.     }`
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Vector3 directionInput = input.normalized;
    MoveThePlayer(directionInput); this is the issue. you are only setting directionInput once, and it doesn't care about the key that was pressed.

    put a Debug.Log(directionInput); to see what you are setting it to. I bet it doesn't change
     
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I was wrong the values are changing. I'm still looking into the script.
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    change this line : Vector3 velocityMovement = transform.forward * currentSpeed + Vector3.up * velocityY;
    to : Vector3 velocityMovement = moveAmount * 5 * currentSpeed + Vector3.up * velocityY;
    and it will work, but your clamp speed is not getting used.
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    this works for me. you might not like it. there is a slowdown speed. 1.05f
    Code (CSharp):
    1. void MoveThePlayer(Vector3 directionInput)
    2.     {
    3.         Vector3 velocity = directionInput * walkSpeed;
    4.         Vector3 moveAmount = velocity * Time.deltaTime;
    5.  
    6.         moveAmount = transform.TransformDirection(moveAmount);
    7.  
    8.         float targetSpeed = walkSpeed * directionInput.magnitude;
    9.  
    10.         currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
    11.  
    12.         Vector3 velocityMovement = moveAmount + charController.velocity;
    13.  
    14.         charController.Move(velocityMovement * Time.deltaTime / 1.05f);
    15.  
    16.         velocityY += Time.deltaTime * gravity;
    17.         currentSpeed = new Vector3(charController.velocity.x, 0, charController.velocity.z).magnitude;
    18.         if (charController.isGrounded)
    19.         {
    20.             velocityY = 0;
    21.         }
    22.     }
     
    BluesyPompanno likes this.
  6. BluesyPompanno

    BluesyPompanno

    Joined:
    Jan 1, 2017
    Posts:
    37
    Ha it works !. Thank you :D
     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    np, anytime!