-2

hey I am quite new to coding and I'm trying to right a simple player movement script using rigidbody. I have been able to make my player move and turn to face its movement direction I just cannot figure out how to make it do it smoothly . I have made a float for turnspeed/smoothing but cannot figure out how to implement it into my code. sorry if my codes messy or wrong I am very new to this and would love some constructive advice cheers .

my code :

public float smoothing = .1f;

public void Update()
    {       
        movement = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical")).normalized;

private void FixedUpdate()
    {
        moveCharacter(movement);

private void moveCharacter(Vector3 direction)
    {
        // player look direction
        Vector3 lookDirection = movement + gameObject.transform.position;
        gameObject.transform.LookAt(lookDirection);



        // movement
         rigidBodyComponant.MovePosition(transform.position + (direction * speed * Time.deltaTime));
    }
Ruzihm
  • 19,749
  • 5
  • 36
  • 48

1 Answers1

0

For smoothing out position changes I like to use Lerping. This for example, can be used to lerp the rotation.

 Quaternion toRotation = Quaternion.FromToRotation(transform.forward, direction);
 transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed * Time.deltaTime);

You should put this in the FixedUpdate() for smooth results. You can decide the speed yourself by giving it a certain value.

Rowin
  • 121
  • 2
  • 11
  • so i tried lerping but for the life of me could not make the code work for my situation . my game has a single placed camera. not one that follows the player and i tried your suggested code and added my own speed float to if but instead of smoothly turning the character to the direction that it is moving the player model snaps instantly to the direction then smoothly turns back to face away from the camera – banditsouls Jan 28 '22 at 05:50