Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Help, player only jumping occasionally :(

Discussion in 'Scripting' started by OleksandrNi, Apr 3, 2024.

  1. OleksandrNi

    OleksandrNi

    Joined:
    Apr 2, 2024
    Posts:
    1
    Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     private CharacterController controller;
    8.     private Vector3 direction;
    9.     public float forwardSpeed;
    10.  
    11.     private int desiredLane = 1;//0:left 1:middle 2:right
    12.     public float laneDistance = 4;// distance beetween two lanes
    13.  
    14.     public float JumpForce;
    15.     public float Gravity = -20;
    16.     void Start()
    17.     {
    18.         controller = GetComponent<CharacterController>();
    19.     }
    20.  
    21.     private void Jump()
    22.     {
    23.         direction.y = JumpForce;
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         direction.z = forwardSpeed; {}
    30.  
    31.         //Gather the imputs on which lane we should be
    32.  
    33.         if(Input.GetKeyDown(KeyCode.RightArrow))
    34.         {
    35.             desiredLane++;
    36.             if(desiredLane==3)
    37.             desiredLane = 2;
    38.         }
    39.        
    40.         if(Input.GetKeyDown(KeyCode.LeftArrow))
    41.         {
    42.             desiredLane--;
    43.             if(desiredLane == -1)
    44.             desiredLane = 0;
    45.         }
    46.  
    47.         //Calculate where we should be on the future
    48.  
    49.         Vector3 targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;
    50.  
    51.         if (desiredLane ==0)
    52.         {
    53.             targetPosition += Vector3.left * laneDistance;
    54.  
    55.         }else if (desiredLane ==2)
    56.         {
    57.             targetPosition += Vector3.right * laneDistance;
    58.         }
    59.  
    60.         transform.position = Vector3.Lerp(transform.position, targetPosition,50 * Time.fixedDeltaTime);
    61.  
    62.         if(controller.isGrounded)
    63.         {  
    64.            
    65.             if(Input.GetKeyDown(KeyCode.UpArrow))
    66.            {
    67.             Jump();
    68.  
    69.            }
    70.         }
    71.        
    72.         else
    73.         {
    74.             direction.y += Gravity * Time.deltaTime;
    75.         }
    76.        
    77.        
    78.          
    79.  
    80.      
    81.        
    82.     }
    83.  
    84.  
    85.     private void FixedUpdate()
    86.     {
    87.         controller.Move(direction*Time.fixedDeltaTime);  
    88.     }
    89.  
    90.    
    91. }
    92.  
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,055
    The mistake you're making is that you're trying to move your character around with controller.Move and transform.position at the same time. Instead you should just use controller.Move from within Update, not FixedUpdate.

    Code (CSharp):
    1. using UnityEngine;
    2. public class PlayerController : MonoBehaviour
    3. {
    4.     private CharacterController controller;
    5.     private Vector3 velocity;
    6.     public float forwardSpeed;
    7.     private int desiredLane = 1;//0:left 1:middle 2:right
    8.     public float laneDistance = 4;// distance beetween two lanes
    9.     public float JumpForce;
    10.     public float Gravity = -20;
    11.     float leftLanePosition;
    12.  
    13.     void Start()
    14.     {
    15.         controller = GetComponent<CharacterController>();
    16.         leftLanePosition=transform.position.x-laneDistance;  // Get the left lane position by assuming the player is starting in the middle lane
    17.         velocity.z = forwardSpeed;
    18.     }
    19.     void Update()
    20.     {
    21.         if (Input.GetKeyDown(KeyCode.RightArrow) && desiredLane<2)
    22.             desiredLane++;
    23.         else if (Input.GetKeyDown(KeyCode.LeftArrow) && desiredLane>0)
    24.             desiredLane--;
    25.         float targetPosition = leftLanePosition + desiredLane * laneDistance; // Calculate where we should be in the future
    26.  
    27.         velocity.x=(targetPosition-transform.position.x)*10; // Set velocity x so we head towards the target
    28.  
    29.         if (controller.isGrounded)
    30.         {
    31.             if (Input.GetKeyDown(KeyCode.UpArrow))
    32.                 velocity.y = JumpForce;
    33.         }
    34.         else
    35.             velocity.y += Gravity * Time.deltaTime;
    36.  
    37.         controller.Move(velocity * Time.deltaTime);
    38.     }
    39. }
     
    Last edited: Apr 3, 2024