Sorry for my bad english but i have some question.
I’m making a racing game with 2 lanes. Now I want the car to move forward in a straight line and when I hold down the left mouse button, the car will start moving gradually to the left (second lane). 2) and when the object moves to the middle of the 2nd lane, it will move in a straight line. When you release the left mouse button, the car will gradually move back to the first lane. I tried doing this but it doesn’t work.
using UnityEngine;
public class Move : MonoBehaviour
{
private Rigidbody rb;
public float speed;
private Vector3 dir;
private bool goLeft;
private bool goRight;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
dir = Vector3.forward;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
dir = new Vector3(-1f, 0, 1f);
return;
}
if (Input.GetMouseButtonUp(0))
{
dir = new Vector3(1f, 0, 1f);
return;
}
if (transform.position.x > -4f && transform.position.x < -2f)
{
dir = Vector3.forward;
}
}
private void FixedUpdate()
{
if (Input.GetMouseButtonDown(0))
{
dir = new Vector3(-1f, 0, 1f);
if(transform.position.x > -4f && transform.position.x < -2f)
{
dir = Vector3.forward;
}
rb.velocity = dir * Time.fixedDeltaTime * speed;
return;
}
if (Input.GetMouseButtonUp(0))
{
dir = new Vector3(1f, 0, 1f);
return;
}
if (transform.position.x > -1f && transform.position.x < 1f)
{
dir = Vector3.forward;
rb.velocity = dir * Time.fixedDeltaTime * speed;
}
}
}