Hello my script is below. How would i be able to make the car not go left when going right, vice versa.
Same as up down. So the only way to move in those directions is to turn the car.
The mechanic i`m going for is similar to super pixel racers/Showdown miami.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Drive : MonoBehaviour
{
public Rigidbody2D rb;
public Animator anim;
public float moveSpeed;
public float x, y;
public bool moving;
private Vector3 moveDir;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
x = Input.GetAxisRaw("Horizontal");
y = Input.GetAxisRaw("Vertical");
if (x != 0 || y != 0)
{
anim.SetFloat("X", x);
anim.SetFloat("Y", y);
if (Input.GetAxisRaw("Horizontal") > 1 || (Input.GetAxisRaw("Horizontal") < 1) || (Input.GetAxisRaw("Vertical") > 1 || (Input.GetAxisRaw("Vertical") < 1)))
{
moving = true;
anim.SetBool("isMoving", moving);
}
}
else
{
moving = false;
StopMoving();
}
moveDir = new Vector3(x, y).normalized;
}
private void FixedUpdate()
{
if (moving == true)
{
rb.velocity = moveDir * moveSpeed * Time.deltaTime;
}
}
private void StopMoving()
{
rb.velocity = Vector3.zero;
rb.Sleep();
}
}