Search Unity

Resolved Move rigidbody at constant speed while also applying external forces

Discussion in 'Physics' started by pappalardodd, May 21, 2020.

  1. pappalardodd

    pappalardodd

    Joined:
    Apr 13, 2020
    Posts:
    96
    I open this topic because, I tried to find a solution everywhere but without success and I hope to get help.
    I am trying to move a rigidbody at a fixed speed, but which is also affected by other external forces that it may suffer.
    Below I delete my script and all the solutions that I tried to use but they didn't work:

    Code (CSharp):
    1. public class PlayerController2 : MonoBehaviour
    2. {
    3.    private Rigidbody rb;
    4.    private Vector3 movement;
    5.   private float speed = 3;
    6.    public float JumpForce = 200;
    7.  
    8. void Start()
    9.     {
    10.       rb = GetComponent<Rigidbody>();
    11.     }
    12.  
    13. void Update()
    14. {
    15. float Horizontal = Input.GetAxis("Horizontal");
    16. float Vertical = Input.GetAxis("Vertical");
    17. movement = (transform.right * Horizontal + transform.forward * Vertical) * speed;
    18.  
    19. if(isGrounded && Input.GetKeyDown(KeyCode.Space))
    20.        rb.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
    21.      
    22.  
    23. RaycastHit hit;
    24. if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, rayDistance, layer))
    25. isGrounded = true;
    26. else isGrounded = false;
    27.  
    28. }
    29.  
    30.    private void FixedUpdate ()
    31.   {    
    32.  
    33.    }
    34. }

    Code (CSharp):
    1. rb.velocity = movement;
    The object moves at the desired speed of movement, but any other force suffered is canceled (for example, if it jumping or suffer other forces, they are canceled)


    Code (CSharp):
    1. if(rb.velocity.sqrMagnitude < speed * speed)
    2. rb.AddForce(movement, ForceMode.VelocityChange);
    The object can jump, it is sensitive to external forces, but the speed with which it moves oscillates and is never fixed


    Code (CSharp):
    1. Vector3 deltaV = movement - rb.velocity;
    2. Vector3 accel = deltaV/Time.deltaTime;
    3. rb.AddForce(accel, ForceMode.Acceleration);
    Adds an AddForce that causes the object to have a fixed speed, but is not affected by other forces such as jumping (this I don't understand because I'm adding a force it should add to other forces if are present, ... it's right?)

    Code (CSharp):
    1. Vector3 forex;
    2.  
    3. if(isGrounded && Input.GetKeyDown(KeyCode.Space))
    4.   forex = Vector3.up;
    5.   rb.AddForce(forex * JumpForce, ForceMode.Impulse);
    6.  
    7. Vector3 deltaV = movement + forex - rb.velocity;
    8.   Vector3 accel = deltaV/Time.deltaTime;
    9. rb.AddForce(accel, ForceMode.Acceleration);
    I tried to add external forces such as jumping, through a vector so that accelerates also calculates the speed caused by external forces and not only that of the movements. The result is that when the object jumps it does not stop but continues to rise, so I was wrong