Search code examples
c#unity-game-enginemathvectormotion

How to smooth the change of player's direction?


I want to smooth the change of player's direction. I have a simple motion script, but when I go ahead and start going backwards, I want my character to start "sliding". There's an example on gif file - https://i.sstatic.net/FIRA5.jpg. I tried to do it but it's crazy(

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Moving : MonoBehaviour
{
    [SerializeField]
    Transform frogTransform;
    Vector3 now = new Vector3();
    Vector3 Now
    {
        get
        {
            if (now == null)
            {
                return Vector3.zero;
            }
            else
            {
                return now;
            }
        }
    }

    void FixedUpdate()
    {
        if (Input.GetKey(KeyCode.W))
        {
            now = Vector3.Lerp(now, Vector3.forward, 0.5f); //this doesn't help me (nothing changes)
            frogTransform.Translate(now * 0.1f);
            now = Vector3.forward;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            now = Vector3.Lerp(now, Vector3.back, 0.5f);
            frogTransform.Translate(now * 0.1f);
            now = Vector3.back;
        }
        if (Input.GetKey(KeyCode.D))
        {
            now = Vector3.Lerp(now, Vector3.right, 0.5f);
            frogTransform.Translate(now * 0.1f);
            now = Vector3.right;
        }
        else if (Input.GetKey(KeyCode.A))
        {
            now = Vector3.Lerp(now, Vector3.left, 0.5f);
            frogTransform.Translate(now * 0.1f);
            now = Vector3.left;
        }
    }
}

Solution

  • From your answer I see you're using Transform.Translate and that is not the tool to apply Unity Physics (and create effects as sliding).

    To apply the slide effect you can add a Rigidbody to your gameobject.

    Then you can use Rigidbody.AddForce to direct your movement.

    As soon as you change the direction/force you will see the sliding effect. Consider that you can tweak the mass and the drag of your rigidbody to have different kind of sliding effects.

    Your code would become.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Moving : MonoBehaviour
    {
        [SerializeField] Rigidbody rigidbody;
    
        [SerializeField] float accelerationForce = 5f;
    
        void FixedUpdate()
        {
            if (Input.GetKey(KeyCode.W))
                rigidbody.AddForce(Vector3.forward * accelerationForce, ForceMode.Impulse);
    
            if (Input.GetKey(KeyCode.S))
                rigidbody.AddForce(Vector3.back * accelerationForce, ForceMode.Impulse);
    
            if (Input.GetKey(KeyCode.D))
                rigidbody.AddForce(Vector3.right * accelerationForce, ForceMode.Impulse);
    
            if (Input.GetKey(KeyCode.A))
                rigidbody.AddForce(Vector3.left * accelerationForce, ForceMode.Impulse);
        }
    }
    

    You also can check this tutorial and this other tutorial.