Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Need help smoothing player movement

Discussion in 'Scripting' started by JayFury55, Aug 31, 2020.

  1. JayFury55

    JayFury55

    Joined:
    Aug 21, 2020
    Posts:
    16
    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:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.    
    8.     [SerializeField] private float speed;
    9.  
    10.     void FixedUpdate()
    11.     {
    12.  
    13.         Vector3 mousePos = Input.mousePosition;
    14.         mousePos.z = 6f;
    15.         //transform.position = Camera.main.ScreenToWorldPoint(mousePos);
    16.         Vector3 targetPos = Camera.main.ScreenToWorldPoint(mousePos);
    17.         transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
    18.     }
    19. }
    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!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,933
    JayFury55 likes this.
  3. JayFury55

    JayFury55

    Joined:
    Aug 21, 2020
    Posts:
    16
    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!
     
    PraetorBlue likes this.
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,933
    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).
     
  5. JayFury55

    JayFury55

    Joined:
    Aug 21, 2020
    Posts:
    16
    It currently looks like this:
    Code (csharp):
    1.  
    2.  transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, smoothTime, speed * Time.deltaTime);
    3.  
    You're saying I don't need to put Time.deltaTime there? And should I put it in Update or Fixed Update?
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,933
    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.
     
  7. JayFury55

    JayFury55

    Joined:
    Aug 21, 2020
    Posts:
    16
    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
     
    PraetorBlue likes this.
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,933
    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.