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 !
using UnityEngine;
public class RowPlayerMovement : MonoBehaviour {
public Rigidbody rb;
private bool left = false;
public float smoothTime = 0.3f;
public Vector3 velocity;
void Start() {
velocity = Vector3.zero;
}
void Update() {
if (Input.GetKeyDown(KeyCode.Q)) {
left = true;
}
}
void FixedUpdate() {
if (left == true) {
MovePlayerLeft();
}
}
void MovePlayerLeft() {
rb.MovePosition(transform.position + Vector3.SmoothDamp(transform.position, Vector3.left, ref velocity, 0.3f));
// OR
// transform.Translate(Vector3.SmoothDamp(transform.position, Vector3.left, ref velocity, smoothTime));
Debug.Log("going left");
left = false;
}
}
//
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
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.
using UnityEngine;
public class LaneMovement : MonoBehaviour {
private int desiredLane = 2;
private int currentLane = 2;
public Rigidbody rb;
public float speed = 5f;
private float laneDistance = 2f;
private void Update() {
//gathering inputs
if (Input.GetKeyDown(KeyCode.Q)) {
MoveLane(false);
}
if (Input.GetKeyDown(KeyCode.D)) {
MoveLane(true);
}
//calculate where we should be in the future
Vector3 targetPosition = transform.position.z * Vector3.forward;
if (desiredLane < currentLane) {
targetPosition += Vector3.left * laneDistance;
}
else if (desiredLane > currentLane) {
targetPosition += Vector3.right * laneDistance;
}
// calculate move delta
Vector3 moveVector = Vector3.zero;
moveVector.x = (targetPosition - transform.position).normalized.x * speed;
moveVector.y = 0f;
moveVector.z = speed;
//move the Cube
rb.MovePosition(transform.position + moveVector * Time.deltaTime);
}
private void MoveLane(bool goingRight) {
desiredLane += goingRight ? 1 : -1;
desiredLane = Mathf.Clamp(desiredLane, 0, 4);
}
}
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…
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.