Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Controller is quite sluggish after adding new scripts

Discussion in 'Scripting' started by TovinTheCowardly, Jul 21, 2020.

  1. TovinTheCowardly

    TovinTheCowardly

    Joined:
    Apr 22, 2020
    Posts:
    28
    Hello to all,

    When i created the controller everything worked well. Needed to only have about 10-15 force to get the player moving well. But after adding more code to both the controller and triggers and colliders i need to add about 400f of force and it jusgt doesnt move as well as it did when i first made it....

    controller code:
    Code (CSharp):
    1.  
    2. public class Controller : MonoBehaviour
    3. {
    4.     Rigidbody rb;
    5.     float xInput, zInput;
    6.     public float speed;
    7.     public float jumpforce;
    8.     public float DashForce;
    9.     public LayerMask GroundLayer;
    10.     public SphereCollider Col;
    11.  
    12.     float fallmult = 2.5f;
    13.     float lowjump = 2f;
    14.  
    15.  
    16.     bool jump = false;
    17.  
    18.     private void Awake()
    19.     {
    20.         rb = GetComponent<Rigidbody>();
    21.         Col = GetComponent<SphereCollider>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         xInput = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
    28.         //zInput = Input.GetAxis("Vertical") * speed * Time.deltaTime;
    29.  
    30.  
    31.         if (IsGrounded() && Input.GetButtonDown("Jump"))
    32.         {
    33.             jump = true;
    34.         }
    35.  
    36.  
    37.  
    38.  
    39.         if (rb.velocity.y < 0)
    40.         {
    41.             rb.velocity += Vector3.up * Physics.gravity.y * (fallmult - 1) * Time.deltaTime;
    42.         }
    43.         else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
    44.         {
    45.             rb.velocity += Vector3.up * Physics.gravity.y * (lowjump - 1) * Time.deltaTime;
    46.  
    47.         }
    48.     }
    49.  
    50.     private void FixedUpdate()
    51.     {
    52.         rb.velocity = new Vector3(xInput, rb.velocity.y, 0);
    53.  
    54.         if (jump)
    55.         {
    56.             Jumping();
    57.             jump = false;
    58.         }
    59.  
    60.         if (Input.GetKeyDown(KeyCode.LeftShift))
    61.         {
    62.             rb.AddForce(DashForce* Time.deltaTime, 0, 0, ForceMode.Impulse);
    63.         }
    64.     }
    65.  
    66.  
    67.     void Jumping()
    68.     {
    69.         rb.AddForce(Vector3.up * jumpforce * Time.deltaTime, ForceMode.Impulse);
    70.  
    71.        
    72.     }
    73.  
    74.      private bool IsGrounded()
    75.     {
    76.         return Physics.CheckCapsule(Col.bounds.center, new Vector3(Col.bounds.center.x, Col.bounds.min.y, Col.bounds.center.z), Col.radius * 0.9f, GroundLayer);
    77.     }
    78.    
    79. }
    80.  
    ive checked various tutorials and the code i use is quitre similar to what others use, and i dont see where the issue could be coming from.

    Is there anthing in my code that i missed that would make the ocntrols slow/sluggish?

    Best Regards,
    Przemyslaw

    p.s the
    Code (CSharp):
    1.  Input.GetKeyDown(KeyCode.LeftShift)
    doesnt seem to work unless i add ridiculous amounts of force but when i do that the player kinda "teleports".

    But when i add to the dash as
    Code (CSharp):
    1.  Input.GetKey(KeyCode.LeftShift)
    it works pretty well. But the problem is that as long as they hold LShift they keep having force added :/
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    6,135
    You're directly changing the velocity of the Rigidbody in Update which is going to completely trample over any forces you may be adding in FixedUpdate. You're also trying to capture a KeyDown event in FixedUpdate which is liable to cause missed inputs.

    In summary:
    1. Pick one, adding forces or setting velocity. But not both, they will fight with each other
    2. Only do Rigidbody operations in FixedUpdate.
    3. Only capture input in Update().
    To reconcile 2. and 3. the general approach is to capture the input in Update and consume it in FixedUpdate using instance variables like you're already doing with
    jump
    .
     
    Joe-Censored likes this.
  3. TovinTheCowardly

    TovinTheCowardly

    Joined:
    Apr 22, 2020
    Posts:
    28
    Hmmmm, ok... So ive removed any add force and im going to stick just with adding velocities. I ve also removed all KeyDown events in Fixed-Update. Ive alse removed trying to dash until i understand what im doing wrong. My current conde looks like this, and i think ive fixed what you said was incorrect, yet the controls are still the same.

    Code (CSharp):
    1.  
    2. public class Controller : MonoBehaviour
    3. {
    4.     Rigidbody rb;
    5.     float xInput, zInput;
    6.  
    7.     public float speed;
    8.     public float jumpforce;
    9.  
    10.     public LayerMask GroundLayer;
    11.     public SphereCollider Col;
    12.  
    13.     float fallmult = 2.5f;
    14.     float lowjump = 2f;
    15.  
    16.  
    17.     bool jump = false;
    18.    
    19.     private void Awake()
    20.     {
    21.         rb = GetComponent<Rigidbody>();
    22.         Col = GetComponent<SphereCollider>();
    23.     }
    24.  
    25.  
    26.     void Update()
    27.     {
    28.         xInput = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
    29.  
    30.        
    31.  
    32.  
    33.  
    34.  
    35.  
    36.         if (IsGrounded() && Input.GetButtonDown("Jump"))
    37.         {
    38.             jump = true;
    39.         }
    40.  
    41.  
    42.         if (rb.velocity.y < 0)
    43.         {
    44.             rb.velocity += Vector3.up * Physics.gravity.y * (fallmult - 1) * Time.deltaTime;
    45.         }
    46.         else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
    47.         {
    48.             rb.velocity += Vector3.up * Physics.gravity.y * (lowjump - 1) * Time.deltaTime;
    49.         }
    50.  
    51.  
    52.     }
    53.  
    54.     private void FixedUpdate()
    55.     {
    56.         rb.velocity = new Vector3(xInput, rb.velocity.y, 0);
    57.  
    58.         if (jump)
    59.         {
    60.             Jumping();
    61.             jump = false;
    62.         }
    63.  
    64.        
    65.     }
    66.  
    67.  
    68.     void Jumping()
    69.     {
    70.         //rb.AddForce(Vector3.up * jumpforce * Time.deltaTime, ForceMode.Impulse);
    71.         rb.velocity = new Vector3(0, jumpforce * Time.deltaTime, 0);      
    72.     }
    73.  
    74.        private bool IsGrounded()
    75.     {
    76.         return Physics.CheckCapsule(Col.bounds.center, new Vector3(Col.bounds.center.x, Col.bounds.min.y, Col.bounds.center.z), Col.radius * 0.9f, GroundLayer);
    77.     }
    78.  
    79.    
    80. }
    81.  
     
  4. TovinTheCowardly

    TovinTheCowardly

    Joined:
    Apr 22, 2020
    Posts:
    28
    I had also tried doing to the horizantal movement the same as i did with jump, and that was slightly better i only need to put the speed variable to 250 to get movement, which is down by 150.

    Code (CSharp):
    1.  
    2. public class Controller : MonoBehaviour
    3. {
    4.     Rigidbody rb;
    5.     float xInput, zInput;
    6.  
    7.     public float speed;
    8.     public float jumpforce;
    9.  
    10.     public LayerMask GroundLayer;
    11.     public SphereCollider Col;
    12.  
    13.     float fallmult = 2.5f;
    14.     float lowjump = 2f;
    15.  
    16.     bool move = false;
    17.     bool jump = false;
    18.    
    19.     private void Awake()
    20.     {
    21.         rb = GetComponent<Rigidbody>();
    22.         Col = GetComponent<SphereCollider>();
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.        
    29.         //zInput = Input.GetAxis("Vertical") * speed * Time.deltaTime;
    30.  
    31.         if (Input.GetButton("Horizontal"))
    32.         {
    33.             move = true;
    34.         }
    35.         else if (Input.GetButtonUp("Horizontal"))
    36.         {
    37.             move = false;
    38.         }
    39.  
    40.        
    41.  
    42.  
    43.         if (IsGrounded() && Input.GetButtonDown("Jump"))
    44.         {
    45.             jump = true;
    46.         }
    47.  
    48.  
    49.         if (rb.velocity.y < 0)
    50.         {
    51.             rb.velocity += Vector3.up * Physics.gravity.y * (fallmult - 1) * Time.deltaTime;
    52.         }
    53.         else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
    54.         {
    55.             rb.velocity += Vector3.up * Physics.gravity.y * (lowjump - 1) * Time.deltaTime;
    56.         }
    57.  
    58.  
    59.     }
    60.  
    61.     private void FixedUpdate()
    62.     {
    63.        
    64.  
    65.         if (move)
    66.         {
    67.             Move();
    68.         }
    69.         else if (move==false)
    70.         {
    71.             NoMove();
    72.         }
    73.  
    74.  
    75.         if (jump)
    76.         {
    77.             Jumping();
    78.             jump = false;
    79.         }
    80.  
    81.        
    82.     }
    83.     void Move()
    84.     {
    85.         xInput = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
    86.         rb.velocity = new Vector3(xInput, rb.velocity.y, 0);
    87.     }
    88.  
    89.     void NoMove()
    90.     {
    91.         rb.velocity = new Vector3(0, rb.velocity.y, 0);
    92.     }
    93.  
    94.  
    95.     void Jumping()
    96.     {
    97.         //rb.AddForce(Vector3.up * jumpforce * Time.deltaTime, ForceMode.Impulse);
    98.         rb.velocity = new Vector3(0, jumpforce * Time.deltaTime, 0);      
    99.     }
    100.  
    101.        private bool IsGrounded()
    102.     {
    103.         return Physics.CheckCapsule(Col.bounds.center, new Vector3(Col.bounds.center.x, Col.bounds.min.y, Col.bounds.center.z), Col.radius * 0.9f, GroundLayer);
    104.     }
    105.  
    106.    
    107. }
    108.  
     
unityunity