Need help smoothing player movement

I’m learning C# and Unity and I’m creating a game where I want to move the player by using my mouse. I don’t want it to snap to the mouse position, I want it to follow the cursor. I’ve got the basic script that gives me what I want, except for one thing: the player object is currently following the cursor at a constant speed, but I want it to start moving quickly and then gently slow down until it reaches the cursor.
My script looks as follows:

using System;
using System.Collections;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
   
    [SerializeField] private float speed;

    void FixedUpdate()
    {

        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 6f;
        //transform.position = Camera.main.ScreenToWorldPoint(mousePos);
        Vector3 targetPos = Camera.main.ScreenToWorldPoint(mousePos);
        transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
    }
}

I think, in order to achieve the desired effect, I have to exchange the constant speed variable for a logarithmic function; I just don’t know how to implement this.
My current idea was to somehow get start and endpoint of the mouse movement (basically as dx) and use it in a log function with (e^{speed})/dx , but I don’t know how to write that in code, either…

Your help is greatly appreciated!

One quick thing you can try is switching from Vector3.MoveTowards to Vector3.SmoothDamp. That will give you a more smooth movement with acceleration and deceleration:

1 Like

Thank you ^^
I didn’t know how this method works, so I had to think about it a little bit. But after experimenting and setting the speed quite high, it does pretty much exactly what I need it to do. That was quick, thanks again!

1 Like

Make sure you’re not factoring in Time.deltaTime to any of the parameters for SmoothDamp. That might explain why you have to set the speed very high. Unlike MoveTowards, SmoothDamp already takes Time.deltaTime into account automatically (or as the 6th parameter to the function).

It currently looks like this:

 transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, smoothTime, speed * Time.deltaTime);

You’re saying I don’t need to put Time.deltaTime there? And should I put it in Update or Fixed Update?

Correct. The 5th parameter is a “maxSpeed” parameter. You don’t want deltaTime multiplied by it, that essentially will divide your speed by 60 or whatever your frame rate is, which is why you had to pump the speed up so high to get it working right. If you look at https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html you can see that deltaTime is already accounted for by default in the 6th parameter.

Update.

Thank you so much! I’ve actually made progress in the meantime and I’m slowly getting the hang of C#… I’ll let you know when the game is most of the way finished, if you want to have a look at it. It’s a philosophical interactive experience

1 Like

I’d love to take a look when you feel it’s in a state to be looked at! Feel free to drop me a message when that time comes.