Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by ItzPorkchop · Aug 07, 2021 at 03:07 AM · 2dunity 2dmovement script

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++;
         }
     }
 }
 
Comment
Add comment · Show 1
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 logicandchaos · Aug 07, 2021 at 05:05 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
     }

 }

}

Comment
Add comment · 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

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

285 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 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 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

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


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