I’ve created this movement script which controls switching lanes in a 3-lane system, but noticed that where the x-value of the player should either be -2, 0, or 2 (This is where I get the velocity of (-)8 for 1/4 of a second), it is within a range of +/- .05, and the gap just gets bigger to either side.
How could I go about getting an exact x-value from my player’s movement?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movePlayer : MonoBehaviour
{
public KeyCode moveL;
public KeyCode moveR;
public float horizVel = 0;
public int laneNum = 2;
private string controlLocked = "n";
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GetComponent<Rigidbody>().velocity = new Vector3(horizVel, 0, 4);
if (Input.GetKeyDown(moveL) && laneNum > 1 && controlLocked == "n")
{
horizVel = -8;
StartCoroutine(stopSlide());
laneNum -= 1;
controlLocked = "y";
}
if (Input.GetKeyDown(moveR) && laneNum < 3 && controlLocked == "n")
{
horizVel = 8;
StartCoroutine(stopSlide());
laneNum += 1;
controlLocked = "y";
}
}
IEnumerator stopSlide()
{
yield return new WaitForSeconds(0.25f);
horizVel = 0;
controlLocked = "n";
}
}