Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cryno1000 · Jan 21, 2020 at 11:07 PM · booleandebugdebugging

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");
     }
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cryno1000 · Jan 22, 2020 at 04:05 AM 0
Share

Thanks, can't believe that problem had such a simple solution!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

124 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges