Search Unity

Question Changing rigidbody.MovePosition to AddForce and/or better player movement?

Discussion in 'Physics' started by CatDadJynx, Aug 9, 2021.

  1. CatDadJynx

    CatDadJynx

    Joined:
    Apr 16, 2020
    Posts:
    249
    Hello, my game is currently set up for my player input to move my player via rigidbody.MovePosition, however when using a keyboard this makes the player movement a little stiff (since its a 3D platformer)... How would I go about changing my rigibody movment to use AddForce and/or how would I make my movement controller a little more fluid (so that my player movement uses a gradual change to maximum acceleration or something, for example)?

    Sorry, I know this question is fairly basic but Ive been having difficulty just changing my code to use AddForce instead (in order to test out different force modes, at least). Thanks!

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. namespace Player
    7. {
    8.     [RequireComponent(typeof(Rigidbody))]
    9.     public class PlayerController_RB : MonoBehaviour
    10.     {
    11.  
    12.         // movement
    13.         public float speed = 10.0f;
    14.         private Vector3 moveDirection;
    15.         private Rigidbody playerRigidBody;
    16.  
    17.         public Transform pivot;
    18.  
    19.         private float h;
    20.         private float v;
    21.  
    22.         // Use this for initialization
    23.         void Start()
    24.         {
    25.             playerRigidBody = GetComponent<Rigidbody>();
    26.         }
    27.  
    28.  
    29.  
    30.         // Update is called once per frame
    31.         void Update()
    32.         {
    33.  
    34. #if UNITY_EDITOR || UNITY_STANDALONE
    35.             h = Input.GetAxis("Horizontal");
    36.             v = Input.GetAxis("Vertical");
    37. #endif
    38.             // update move direction
    39.             moveDirection = new Vector3(h, 0, v).normalized;
    40.  
    41.             Vector3 input = Vector3.ClampMagnitude(moveDirection, 1);
    42.  
    43.             Vector3 camF = pivot.forward;
    44.             Vector3 camR = pivot.right;
    45.  
    46.             //remove component in direction up
    47.             camF -= Vector3.Project(camF, pivot.up);
    48.             camR -= Vector3.Project(camR, pivot.up);
    49.             camF = camF.normalized;
    50.             camR = camR.normalized;
    51.  
    52.             moveDirection = (camF * input.z + camR * input.x);
    53.  
    54.         }
    55.  
    56.         void FixedUpdate()
    57.         {
    58.             if (playerMove == true && moveDirection.magnitude != 0)
    59.             {
    60.                 // update movement
    61.                 playerRigidBody.MovePosition(playerRigidBody.position + (moveDirection * speed * Time.fixedDeltaTime));
    62.  
    63.  
    64.  
    65.             }
    66.  
    67.         }
    68.  
    69.     }
    70. }
    71.  
     
    Last edited: Aug 9, 2021