Search Unity

Question I need to Switch from transform.Translate to rb.AddForce, but I don't know how to make it work.

Discussion in 'Physics' started by darkthor73, Sep 19, 2020.

  1. darkthor73

    darkthor73

    Joined:
    Dec 17, 2015
    Posts:
    1
    At the moment I use transform.Translate to move my character, and for clipping/tunneling reasons I need to switch to using physics-based movement.

    The problem is That I have a bunch of things in the game already applying force to the Rigidbody, and I need the force to be snappy and precise (the way it is using Translate).

    The game is based around manipulating gravity, so there are a lot of forces applied to the player. I trimmed the code as much as practical, so a lot of stuff is missing. let me know if you need something it doesn't show. Rest assured that everything works except for the moving through walls while running.

    Code (CSharp):
    1. public class FallMotor : MonoBehaviour
    2. {
    3.     [Header("Movement")]
    4.     public float moveSpeed = 10f;
    5.     public float runSpeed = 10f;
    6.     public bool isSprinting = false;
    7.     public float sprintSpeed = 15f;
    8.     public Vector2 move = Vector2.zero;
    9.  
    10.     [Header("Jump")]
    11.     public bool isJumping = false;
    12.     public float jumpCost = .4f;
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         Vector2 moveInput = controls.GravPlayer.Move.ReadValue<Vector2>();
    17.         Vector2 mouseInput = controls.GravPlayer.Look.ReadValue<Vector2>();
    18.  
    19.         Vector3 moveCharacter = new Vector3(moveInput.x, 0, moveInput.y);
    20.         if (moveCharacter != Vector3.zero)
    21.         {
    22.             rb.transform.Translate(moveCharacter * moveSpeed * Time.fixedDeltaTime);
    23.         }
    24.         rb.AddForce(downDirection * gravitation);
    25.  
    26.         if (energy < 100 && !isJumping && !isBoosting)
    27.         {
    28.             energy += energyRestoreRate;
    29.         }
    30.  
    31.         if (isLashing && anchorPoint != null && energy > 0)
    32.         {
    33.             Vector3 lashDirection = (anchorPoint.position - transform.position).normalized;
    34.  
    35.             if (energy > 0)
    36.             {
    37.                 rb.AddForce(lashDirection * lashPower);
    38.                 energy -= lashCost;
    39.             }
    40.         }
    41.  
    42.         if (isJumping && energy > 0)
    43.         {
    44.             rb.AddForce(-downDirection * gravitation * 2f);
    45.             energy -= jumpCost;
    46.         }
    47.  
    48.         if (!isChangingGravity)
    49.         {
    50.             Vector3 rotateCharacterLR = new Vector3(0, mouseInput.x * lookSensitivity, 0);
    51.             newRotation = rb.rotation * Quaternion.Euler(rotateCharacterLR);
    52.  
    53.             if (rotateCharacterLR != Vector3.zero)
    54.             {
    55.                 rb.MoveRotation(newRotation);
    56.             }
    57.         }
    58.     }
    59. }
    60.  
    I have determined that the best way is to use ForceMode.VelocityChange, but I need the Velocity to be clamped by a public float. I can't get this to work with the other physics in my Player Controller as well as the new input system. Any help would be greatly appreciated!!