- Home /
Player Movement doesn't work, but Debug.Log shows that it should
My player movement was working fine, but then it suddenly stopped working. I have no idea what could have happened, but I don't think I changed anything that would effect it. Here is the PlayerController Script:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Animations;
 
 public class PlayerController : MonoBehaviour
 {
     [Range(0, 2)]
     public int lane;
 
     [Range(0, 3)]
     public int health = 3;
 
     public Vector2 laneTop;
     public Vector2 laneCenter;
     public Vector2 laneBot;
 
     public float score;
     public GameObject explosion;
     public bool playedExplosion = false;
 
     public GameObject restartMenu;
     public GameObject spawner;
     GameObject[] cones;
     public Animator roadAnim;
     public Animator playerAnim;
 
 
     void Start()
     {
         lane = 1;
 
         laneTop = new Vector2(transform.position.x, -1.4f);
         laneCenter = new Vector2(transform.position.x, -2.8f);
         laneBot = new Vector2(transform.position.x, -4f);
         restartMenu.SetActive(false);
         spawner.SetActive(true);
     }
 
 
     void Update()
     {
         if (lane == 0)
         {
             transform.position = new Vector2(transform.position.x, laneTop.y);
         }
         if (lane == 1)
         {
             transform.position = new Vector2(transform.position.x, laneCenter.y);
         }
         if (lane == 2)
         {
             transform.position = new Vector2(transform.position.x, laneBot.y);
         }
 
 
         if (health <= 0 && !playedExplosion)
         {
             GameObject boom = Instantiate(explosion, transform.position, Quaternion.identity);
             roadAnim.SetTrigger("Dead");
             playerAnim.SetTrigger("Dead");
             playedExplosion = true;
             Destroy(boom, 1);
             spawner.SetActive(false);
             cones = GameObject.FindGameObjectsWithTag("Obstacle");
             foreach(GameObject g in cones)
             {
                 Destroy(g);
             }
                 
         }
         if(health <= 0)
         {
             restartMenu.SetActive(true);
         }
         
         if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             Debug.Log("Should move up");
             SwitchLanes(true);
         }
         if(Input.GetKeyDown(KeyCode.DownArrow))
         {
             Debug.Log("should move down");
             SwitchLanes(false);
         }
 
 
     }
 
     //true is up, false is down
     void SwitchLanes(bool up)
     {
         if(up == true && lane != 0)
         {
             lane--;
         }
         if(up == false && lane != 2)
         {
             lane++;
         }
     }
 }
 
looks good to me, try debug logs right in the SwitchLanes method. Debug lane and up.
maybe laneTop, laneCenter, laneBot got changed in the inspector?
Answer by Marioooo · Aug 11, 2021 at 07:22 PM
Hello! this is a bit weird... i can't see any problem but what i found is some mistakes on where to put some parts of the code. so i just remade it hehe, hope this helps
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Animations;
 
 public class PlayerController : MonoBehaviour
 {
 [Range(0, 2)]
 public int lane;
 [Range(0, 3)]
 public int health = 3;
 public Vector2 laneTop;
 public Vector2 laneCenter;
 public Vector2 laneBot;
 public float score;
 public GameObject explosion;
 public bool playedExplosion = false;
 public GameObject restartMenu;
 public GameObject spawner;
 GameObject[] cones;
 public Animator roadAnim;
 public Animator playerAnim;
 void Start()
 {
     lane = 1;
     laneTop = new Vector2(transform.position.x, -1.4f);
     laneCenter = new Vector2(transform.position.x, -2.8f);
     laneBot = new Vector2(transform.position.x, -4f);
     restartMenu.SetActive(false);
     spawner.SetActive(true);
 }
 void Update()
 {
     // first of all, check the input
     if (Input.GetKeyDown(KeyCode.UpArrow))
     {
         Debug.Log("Should move up");
         SwitchLanes(true);
     }
     if (Input.GetKeyDown(KeyCode.DownArrow))
     {
         Debug.Log("should move down");
         SwitchLanes(false);
     }
     // this should be moved to a single VOID cause these checks are unnecesarry here
     // only check inside update method thing that really change on every frame and you can't control, like time or an input.
     // if you have a "take damage" script, event or void, just create a ManageDamage() VOID and put this inside that void, or just move this code to it.
     if (health <= 0 && !playedExplosion)
     {
         GameObject boom = Instantiate(explosion, transform.position, Quaternion.identity);
         roadAnim.SetTrigger("Dead");
         playerAnim.SetTrigger("Dead");
         playedExplosion = true;
         Destroy(boom, 1);
         spawner.SetActive(false);
         cones = GameObject.FindGameObjectsWithTag("Obstacle");
         foreach (GameObject g in cones)
         {
             Destroy(g);
         }
         restartMenu.SetActive(true);
     }
     // ----- until this place, move it to a new void
 }
 //true is up, false is down
 void SwitchLanes(bool up)
 {
     if (up == true && lane != 0)
     {
         lane--;
     }
     if (up == false && lane != 2)
     {
         lane++;
     }
     if (lane == 0)
     {
         transform.position = new Vector2(transform.position.x, laneTop.y);
     }
     else if (lane == 1)
     {
         transform.position = new Vector2(transform.position.x, laneCenter.y);
     }
     else if (lane == 2)
     {
         transform.position = new Vector2(transform.position.x, laneBot.y);
     }
 }
}
Your answer
 
 
             Follow this Question
Related Questions
How do i make a cube move (Continuosly without stopping) when i press a button once in unity 2D 2 Answers
Using UI buttons for Input Manager 0 Answers
How do I enable ShadowCaster2d? 1 Answer
How to Rotate 2D Sprite Towards Moving Direction? 0 Answers
help with a player moving in a grid with obstacles 2d 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                