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 /
  • Help Room /
avatar image
1
Question by TheGameCreator18 · Nov 19, 2016 at 10:41 AM · c#2dgameobject

Player not moving

I'm creating an 2d Game Currently, but my player object just isn't moving like it should, do you have any idea why this could be?

     #region PlayerMovement
         if (PlayerPosY < 199) {
         PlayerPosY = playerInstance.transform.position.y;
         if(! BossArrived){
             playerInstance.transform.Translate (Vector3.up * Time.deltaTime * PlayerSpeed);

         }

             if (Input.GetKey (KeyCode.S)) {
                 if (PlayerSpeed > 2) {
                     PlayerSpeed = PlayerSpeed - 1;
                 } 
             }
             if (Input.GetKey (KeyCode.W)) {
                 if (PlayerSpeed < 10) {
                     PlayerSpeed = PlayerSpeed + 1;            
                 }
             }
         
             if (Input.GetKey (KeyCode.A)) {
                 PlayerPosX -= 0.5f;
             playerInstance.transform.position += -transform.right * Time.deltaTime * PlayerSpeed;
         }

             if (Input.GetKey (KeyCode.D)) {
                 PlayerPosX += 0.5f;
             playerInstance.transform.position += transform.right * Time.deltaTime * PlayerSpeed;
         }
     }

     //REVERSED CONTROL ;D
     if (PlayerPosY >= 198) {
         PlayerPosY = 400;
         if(PlayerRotX == 0) {
             playerInstance.transform.Rotate (180, 0, 0, Space.World);
             PlayerRotX = 1;
         }
             playerInstance.transform.Translate (Vector2.up * Time.deltaTime * PlayerSpeed);
             if (Input.GetKey (KeyCode.S)) {
                 if (PlayerSpeed < 10) {
                     PlayerSpeed = PlayerSpeed + 1;
                 }
             }
             if (Input.GetKey (KeyCode.W)) {
                 if (PlayerSpeed > 2) {
                     PlayerSpeed = PlayerSpeed - 1;            
                 }
             }
         }

             if (Input.GetKey (KeyCode.A)) {
                 PlayerPosX += 0.5f;
                 playerInstance.transform.position += transform.right * Time.deltaTime * PlayerSpeed;

         } 
             if (Input.GetKey (KeyCode.D)) {
                 PlayerPosX -= 0.5f;
             playerInstance.transform.position += -transform.right * Time.deltaTime * PlayerSpeed;
         } 
     if (!playerInstance.GetComponent<SpriteRenderer> ().isVisible) {
         PlayerPosX = 10;
         playerInstance.transform.position = new Vector3 (PlayerPosX, playerInstance.transform.position.y, PlayerPosZ); 
     }    
     if (Input.GetKeyDown (KeyCode.P)) {
         if (Time.timeScale == 1) {
             Time.timeScale = 0;            
         } else if (Time.timeScale == 0) {
             Time.timeScale = 1;            
         }
     }
     if (Input.GetKeyDown (KeyCode.Escape)) {
         Application.Quit ();
     }
     #endregion

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
2
Best Answer

Answer by ThePersister · Nov 19, 2016 at 12:58 PM

Hey @TheGameCreator18,

If you look at the code you posted here, at line 49, your "Reverse Control" if statement ends early!

That means that before reaching Y 199, you'd always Move Left (Normal + Reverse) and Right (Normal + Reverse) at the same time when pressing A or D, Normal + Reverse together meaning No Movement Sideways.

To fix this, move the } to line 59.

I noticed another danger and that is:

PlayerPosY < 199 combined with PlayerPosY >= 198, at player Y == 198, the player will move both normal and reverse, so it won't move.

To fix that, make it an if else statement instead of a double if.

Noticed that you use "transform.right" instead of "playerTransform.right" for moving sideways, which will not make it invert correctly.

Noticed that you're NOT applying PlayerPosX in your sideways movement, even though you set it.

Noticed (During refactoring, fixed that for you in the finished code):

         // When reaching 199, jumps to 400, warning this will lock the player on Y 400.
         PlayerPosY = reverseControl ? 400 : playerTransform.position.y;

Conceptually, I think you have a 2D Vertical Scroll Shooter game, where you move from 0 to 200 and then reverse from 400 to 200, is that correct?

On top of that I cleaned up your code a bit. You can adjust it to your needs, but this should look / feel cleaner.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovementFix : MonoBehaviour {
 
     [Header("Player Reference")]
     public GameObject playerInstance;
     private Transform playerTransform;
 
     [Header("Player Movement")]
     public float PlayerSpeed_Current = 5f;
     public float PlayerSpeed_Min = 2f;
     public float PlayerSpeed_Max = 10f;
     public float PlayerSpeed_Sideways = 0.5f;
     public float PlayerPosX, PlayerPosY, PlayerPosZ,
                  PlayerRotX;
 
     [Header("Other Stats")]
     public bool BossArrived;
 
     void Update () 
     {
         #region PlayerMovement
 
         playerTransform = playerInstance.transform;
 
         // Noticed original danger: PlayerPosY < 199 && PlayerPosY >= 198, at player Y == 198, the player will move both normal and inverse, so it won't move.
         bool reverseControl = PlayerPosY >= 198;
         int sideways = 0;
 
         // Move up, down, left and right. And reversed if reverseControl == true.
         if (Input.GetKey(KeyCode.S)) PlayerSpeed_Current = reverseControl ? PlayerSpeed_Current + 1 : PlayerSpeed_Current - 1;
         if (Input.GetKey(KeyCode.W)) PlayerSpeed_Current = reverseControl ? PlayerSpeed_Current - 1 : PlayerSpeed_Current + 1;
         if (Input.GetKey(KeyCode.A)) sideways = reverseControl ? 1 : -1;
         if (Input.GetKey(KeyCode.D)) sideways = reverseControl ? -1 : 1;
 
         if (reverseControl)
         {
             // Rotate player 180. (Only do this once)
             if (PlayerRotX == 0)
             {
                 PlayerPosY = 400; // Set Y to 400 once.
                 playerTransform.Rotate(180, 0, 0, Space.World);
                 PlayerRotX = 1;
             }
         }
 
         // Makes sure PlayerSpeed is always at least 2 and at most 10.
         PlayerSpeed_Current = Mathf.Clamp(PlayerSpeed_Current, PlayerSpeed_Min, PlayerSpeed_Max);
 
         // Move up or down, but only if there's no boss arrived.
         if (!BossArrived)
         {
             Vector3 upOrDown = (reverseControl ? Vector3.down : Vector3.up);
             playerTransform.Translate(upOrDown * Time.deltaTime * PlayerSpeed_Current);
         }
 
         // Sideways movement.
         if (sideways != 0) 
         {
             bool left = (sideways == -1);
 
             if (left) 
             {
                 playerTransform.position -= playerTransform.right * PlayerSpeed_Sideways * Time.deltaTime;
             }
             else
             {
                 playerTransform.position += playerTransform.right * PlayerSpeed_Sideways * Time.deltaTime;
             }
         }
 
         #endregion
 
         // Check Player Visible, Pause and Exit.
         _checkPlayerVisible();
         _checkTogglePause();
         _checkEscape();
     }
 
     private void _checkPlayerVisible()
     {
         // Reset X to 10 if Player is no longer visible.
         if (!playerInstance.GetComponent<SpriteRenderer>().isVisible)
         {
             PlayerPosX = 10;
             playerTransform.position = new Vector3(PlayerPosX, playerTransform.position.y, PlayerPosZ);
         }
     }
 
     private void _checkTogglePause()
     {
         if (Input.GetKeyDown(KeyCode.P))
         {
             Time.timeScale = (Time.timeScale == 1) ? 0 : 1;
         }
     }
 
     private void _checkEscape()
     {
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             Application.Quit();
         }
     }
 }
 

I hope that helps, if it did, please accept this answer, it'd be much appreciated! If you have any other questions, let me know! :)

Cheers,

ThePersister

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 TheGameCreator18 · Nov 19, 2016 at 01:04 PM 1
Share

Thanks, for the help :D

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

282 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

Related Questions

Scale of object does not update properly with public variable 1 Answer

How can I make the game object follow the mouse? 1 Answer

Set One GameObject Inactive If Another GameObject Is Active? 1 Answer

Find active Child gameobject out of multiple gameobjects 2 Answers

Unity2D C# Randomly Spawn GameObject in an Area over another GameObject 1 Answer


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