Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Beginner - need help with SmoothDamp

Discussion in 'Scripting' started by zZikS, Oct 15, 2020.

  1. zZikS

    zZikS

    Joined:
    Oct 7, 2020
    Posts:
    3
    Hello. I'm a very fresh newbie with Unity and i'm trying to do simple stuff : I have a cube (figuring a car), that is set on a ground with 5 rows. I want that when I press "Q", it moves to the row to its left, and I want it not to be instantaneous but smooth. Si I'm trying out to use Smoothdamp but when I hit Q, the command makes it jump instead of moving on to the X axis. Here is the code. Can you guys tell me what I am missing ? :) Thanks a lot !

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class RowPlayerMovement : MonoBehaviour {
    5.  
    6.     public Rigidbody rb;
    7.     private bool left = false;
    8.     public float smoothTime = 0.3f;
    9.     public Vector3 velocity;
    10.    
    11.  
    12.     void Start() {
    13.         velocity = Vector3.zero;
    14.     }
    15.     void Update() {
    16.          if (Input.GetKeyDown(KeyCode.Q)) {
    17.             left = true;
    18.          }
    19.      }
    20.  
    21.     void FixedUpdate() {
    22.         if (left == true) {
    23.             MovePlayerLeft();
    24.         }
    25.     }
    26.     void MovePlayerLeft() {
    27.         rb.MovePosition(transform.position + Vector3.SmoothDamp(transform.position, Vector3.left, ref velocity, 0.3f));
    28.         // OR
    29.         // transform.Translate(Vector3.SmoothDamp(transform.position, Vector3.left, ref velocity, smoothTime));
    30.         Debug.Log("going left");
    31.         left = false;
    32.     }
    33. }
    34. //
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,144
    You want two variables:

    -
    currentLane

    -
    desiredLane


    When you process user input, you set
    desiredLane
    to what it should be.

    Every
    Update()
    you use
    Mathf.MoveTowards()
    (or
    Vector.MoveTowards()
    ) to move
    currentLane
    gradually to
    desiredLane
    .
     
  3. zZikS

    zZikS

    Joined:
    Oct 7, 2020
    Posts:
    3
    Thanks for the answer. However as I said, I am very newbie, and I tried to play with these functions with no result. My cube doesn't even move. Could be a more explicit please ? It's not easy getting started :p
     
  4. zZikS

    zZikS

    Joined:
    Oct 7, 2020
    Posts:
    3
    Up. I'm really stuck with this one. Tried a new thing, works fines when I have only 3 lanes, can't manage to make it work properly with 5. I'm probably missing something very very simple here... here is the new code.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class LaneMovement : MonoBehaviour {
    5.     private int desiredLane = 2;
    6.     private int currentLane = 2;
    7.     public Rigidbody rb;
    8.     public float speed = 5f;
    9.     private float laneDistance = 2f;
    10.  
    11.     private void Update() {
    12.         //gathering inputs
    13.         if (Input.GetKeyDown(KeyCode.Q)) {
    14.             MoveLane(false);
    15.         }
    16.         if (Input.GetKeyDown(KeyCode.D)) {
    17.             MoveLane(true);
    18.         }
    19.         //calculate where we should be in the future
    20.         Vector3 targetPosition = transform.position.z * Vector3.forward;
    21.         if (desiredLane < currentLane) {
    22.             targetPosition += Vector3.left * laneDistance;
    23.         }
    24.         else if (desiredLane > currentLane) {
    25.             targetPosition += Vector3.right * laneDistance;
    26.         }
    27.         // calculate move delta
    28.         Vector3 moveVector = Vector3.zero;
    29.         moveVector.x = (targetPosition - transform.position).normalized.x * speed;
    30.         moveVector.y = 0f;
    31.         moveVector.z = speed;
    32.  
    33.         //move the Cube
    34.         rb.MovePosition(transform.position + moveVector * Time.deltaTime);
    35.     }
    36.  
    37.     private void MoveLane(bool goingRight) {
    38.  
    39.         desiredLane += goingRight ? 1 : -1;
    40.         desiredLane = Mathf.Clamp(desiredLane, 0, 4);
    41.     }
    42. }
    Problem is that it won't move to the 0 and 4th lane. I'm guessing it is because I have the change the currentLane value somehow, but I don't know where and how to do it. If I add "currentLane--;"' at lane 23 it doesn't move at all. I don't really get it...
     
    Last edited: Oct 18, 2020
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,144
    The problem is the choice of integer values for lanes. They can only either be lane 0, 1 or 2. They can't be in between round numbers. It needs to be a floating point value so that it can be between lanes.

    This type of question comes up so universally I have finally just made a sample package to demonstrate the basic concept. Read my first post above and see if you can correlate each part of the code to the steps indicated.

    Code located here: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4
     

    Attached Files:

    Last edited: Jun 18, 2021
    han73r likes this.
  6. han73r

    han73r

    Joined:
    Nov 18, 2021
    Posts:
    13
    As a solution I've used this one
    Code (CSharp):
    1.     void MovementProcess()
    2.     {
    3.         if (rotatingOn)
    4.         {
    5.             var step = rotateSpeed * Time.deltaTime;
    6.             transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, step);
    7.         }
    8.     }