- Home /
Having an issue with one of my scripts
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");
}
}
Answer by unity_ek98vnTRplGj8Q · Jan 21, 2020 at 11:16 PM
Change your "ifs" to "else ifs" -- when you press the right arrow it is going to check every if statement one right after the other and the conditions will be true every time due to the previous if statement.
Change
if (centerleft == true)
if (center == true)
if (centerright == true)
.
.
.
To
else if (centerleft == true)
else if (center == true)
else if (centerright == true)
.
.
.
Alternatively you can use a switch statement which can be a little bit cleaner
Thanks, can't believe that problem had such a simple solution!
Your answer
Follow this Question
Related Questions
Step/Iterative Debugging 1 Answer
Issue Debugging iPhone with Monodevelopment 1 Answer
How can I track disassembly code back to a script? 0 Answers
Debugging unity application on Nexus 4 5 Answers
How do you Debug Unity Player Builds? 0 Answers