I’m making a game where a player moves to a certain position based on where they are and whether they press the left or right arrow key. However, when the player hits the right arrow, for whatever reason it sets farright to true, no matter where they are. Could someone point whatever is wrong out to me (since I can’t find it). Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playercontrol : MonoBehaviour
{
[SerializeField]
public GameObject bullet;
private Rigidbody2D rb2d;
public bool waitbetweenshots;
float fireRate;
float nextFire;
[SerializeField] public GameObject pausemenu;
public bool farleft;
public bool centerleft;
public bool center;
public bool centerright;
public bool farright;
public bool canmove;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
canmove = true;
center = true;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("space"))
{
Debug.Log("Player tried to shoot");
CheckIfTimeToFire();
}
if (canmove == true)
{
if (farleft == true)
{
transform.position = new Vector3(-3.7f, -4, -0.1f);
if (Input.GetKeyDown(KeyCode.RightArrow))
{
canmove = false;
StartCoroutine("canmovec"); //canmovec stands for can move coroutine
farleft = false;
centerleft = true;
Debug.Log("player moved to centerleft");
}
}
if (centerleft == true)
{
transform.position = new Vector3(-1.9f, -4, -0.1f);
if (Input.GetKeyDown(KeyCode.RightArrow))
{
canmove = false;
StartCoroutine("canmovec");
centerleft = false;
center = true;
Debug.Log("player moved to center");
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
canmove = false;
StartCoroutine("canmovec");
centerleft = false;
farleft = true;
Debug.Log("player moved to farleft");
}
}
if (center == true)
{
transform.position = new Vector3(0, -4, -0.1f);
if (Input.GetKeyDown(KeyCode.RightArrow))
{
canmove = false;
StartCoroutine("canmovec");
center = false;
centerright = true;
Debug.Log("player moved to centerright");
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
canmove = false;
StartCoroutine("canmovec");
center = false;
centerleft = true;
Debug.Log("player moved to centerleft");
}
}
if (centerright == true)
{
transform.position = new Vector3(1.9f, -4, -0.1f);
if (Input.GetKeyDown(KeyCode.RightArrow))
{
canmove = false;
StartCoroutine("canmovec");
centerright = false;
farright = true;
Debug.Log("player moved to farright");
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
canmove = false;
StartCoroutine("canmovec");
centerright = false;
center = true;
Debug.Log("player moved to center");
}
}
if (farright == true)
{
transform.position = new Vector3(3.7f, -4, -0.1f);
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
canmove = false;
StartCoroutine("canmovec");
farright = false;
centerright = true;
Debug.Log("player moved to centerright");
}
}
}
}
IEnumerator canmovec()
{
yield return new WaitForSeconds(0.1f);
canmove = true;
}
private void PauseMenu()
{
Time.timeScale = 0;
}
void CheckIfTimeToFire()
{
if (waitbetweenshots == false)
{
GameObject obj = Instantiate(bullet, transform.position, Quaternion.identity);
bullet bulletScript = obj.GetComponent<bullet>();
bulletScript.movingup = true;
nextFire = Time.time + fireRate;
waitbetweenshots = true;
StartCoroutine("WaitBetweenShotsCounter");
Debug.Log("Player shot");
}
}
IEnumerator WaitBetweenShotsCounter()
{
yield return new WaitForSeconds(0.25f);
waitbetweenshots = false;
Debug.Log("Player can now shoot");
}
}