Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Rigidbody Add Force isn't smooth

Discussion in 'Scripting' started by jasonasaad2, Apr 26, 2021.

  1. jasonasaad2

    jasonasaad2

    Joined:
    Nov 30, 2020
    Posts:
    51
    I am making a Rigidbody FPS Controller, and i'm stuck on jumping. Whenever I add force to jump, it's not smooth. I've tried Add Force, and Velocity but they each have a instant teleport like effect.
    Heres the code

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [RequireComponent(typeof(Rigidbody))]
    7. public class Controller : MonoBehaviour
    8. {
    9.     Rigidbody rb;
    10.     [Header("References")]
    11.     [SerializeField] Transform orientation;
    12.     [Header("Movement")]
    13.     [SerializeField] float moveMultiplier;
    14.     float horizontalMovement;
    15.     float verticalMovement;
    16.     Vector3 moveDirection;
    17.     [SerializeField] float groundDistance;
    18.     [SerializeField] LayerMask groundLayer;
    19.     [Header("Jumping")]
    20.     [SerializeField] float jumpForce;
    21.     [SerializeField] Transform groundCheck;
    22.     public float gravity = 10.0f;
    23.     bool isGrounded;
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {
    27.         rb = GetComponent<Rigidbody>();
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         rb.AddForce(new Vector3 (0, -gravity * rb.mass, 0));
    34.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundLayer);
    35.         verticalMovement = Input.GetAxis("Vertical");
    36.         horizontalMovement = Input.GetAxis("Horizontal");
    37.         moveDirection = orientation.forward * verticalMovement * moveMultiplier + orientation.right * horizontalMovement * moveMultiplier;
    38.         rb.velocity = moveDirection;
    39.         if(isGrounded && Input.GetButtonDown("Jump"))
    40.         {
    41.             Jump();
    42.         }
    43.     }
    44.     void Jump()
    45.     {
    46.         rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    47.     }
    48. }  
    49.  
    50.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    First, you're doing physics in Update()... don't do that. Put it in FixedUpdate(), but gather the input in Update(), particularly line 39 which is an "edge trigger" type input, which will fail in FixedUpdate().

    Second, you might jump by adding force for one frame on line 46, but you're also on the very next frame going to overwrite the velocity in line 38 above.

    Third, your gravity on line 33 would also conflict with your velocity setting in line 38.

    Either use all forces, or do all velocities. The two are hard to mix unless you perhaps separate X/Z axis for velocity control, while passing through the Y axis for jump ballistics and gravity.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Are you at very low framerates?

    You shouldn't be doing all this physics stuff from Update, and your gravity force doesn't make any sense. You're applying gravity in Update, which means at lower framerates you'll get less gravity.
     
  4. jasonasaad2

    jasonasaad2

    Joined:
    Nov 30, 2020
    Posts:
    51
    Thanks with this advice it worked.

    I'm at a high framerate. I didn't know about FixedUpdate, and I fixed the gravity issue.