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
0
Question by balikaa16 · Sep 01, 2014 at 10:54 AM · movestuckx axis

Player gets stuck at position.

Hi guys!

I just downloaded Unity a week ago so I am very much of a newbie... Anyways, I am making a game where the player moves on the x axis and you can change his direction by pressing space. I want him to stop at the edge of the screen and if space is pressed again, move in the opposite direction. For some reason though, once he hits the edge of screen he gets stuck there no matter how many times do I press space.. Please help me guys!!!:(

Here is the script for the player:

 #pragma strict
     
     var speed = 0.03;
     var key : KeyCode;
     var isMoving = true;
     
     
     
     function Update ()
     {
         if(isMoving == true)
         {
         transform.Translate(Vector3.left * speed);
         }
     
         if(Input.GetKeyDown(key))
         {
         isMoving = true;
         speed *= -1;
         
         }
     
         if(transform.position.x < -2.74)
         {
         isMoving = false;
         }
         
         if(transform.position.x > 2.74)
         {
         isMoving = false;
         }
     
     }
     
     

Thanks!!!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by killerstreak · Sep 01, 2014 at 11:50 AM

When you press space you set the var isMoving = true; in the 2nd if statement. However before that variable reaches the 1st if statement(the one that makes the player move) it needs to finish the rest of the code. Because your players position is still > 2.74, var isMoving gets set back to false immediatly (4th if statement), and when the update function is called again.. var isMoving is = false and your player will not move.

its an easy fix. swap the 1st if statement with the second one like this:

 #pragma strict
  
     var speed = 0.03;
     var key : KeyCode;
     var isMoving = true;
  
  
  
 function Update ()
 {
 
   if(Input.GetKeyDown(key))
     {
     isMoving = true; // variable is set to true
     speed *= -1;
  
     }
 
     if(isMoving == true) //look its true! lets execute the code
     {
     transform.Translate(Vector3.left * speed);
     }
  
     if(transform.position.x < -2.74)
     {
     isMoving = false;
     }
  
     if(transform.position.x > 2.74)
     {
     isMoving = false;
     }
  
 }



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
avatar image
0

Answer by Baste · Sep 01, 2014 at 11:26 AM

What is happening is this:

When your player reaches +/- 2.74, the isMoving variable is set to false.

Then, whenever you press space, isMoving is set to true again, and speed is multiplied by -1. BUT, before the player actually moves, the check for wheter the player is at +/- 2.74 fires again, and isMoving gets put back to false before you move the player.

The fix is to put the movement block AFTER the check for space input, and BEFORE the check for if you are on the edge of the map.

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
avatar image
0

Answer by balikaa16 · Sep 01, 2014 at 02:41 PM

Thank you guys! I actually realized the same thing this morning but did not know how to solve it. Thank you very much!!

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 killerstreak · Sep 01, 2014 at 03:05 PM 0
Share

Haha no problem :) but make sure tot select an answer so that this question is closed.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Move child gameobject in x axis (Lerp) 1 Answer

NPC CharacterController doesn't move or stops randomly 0 Answers

how to move or fade GUITextures with iTween 2 Answers

How do you make an object go left and right? 1 Answer

Top-Down Camera Trouble 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