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 Bryangs · Mar 07, 2017 at 03:17 AM · c#scripting problemmovementignore

Script is moving player to the wrong position

Hey guys! I made a simple and sincerely bad written script to just move my Player based on his position. My script has 4 Vectors, and it was supposed to work like: If the player is in Vector 1 and he press "D", then he goes to Vector 2. If he is in Vector 2 and press "A" he goes to Vector 1.

The script works fine with the LEFT side (Pressing "A"), but the RIGHT side (Pressing "D") moves the Player to the wrong position! I want him to like, if he is 0 and wants to go to 4, he will go to 1, then 2, then 3, and finally 4. The problem is that it's just making him immediately go to from 0 to 4 and so on, completely ignoring the "steps". I know this is hard to understand, so if you could test it in your own pc, i am sure you will understand. The script is long, but it's because i like clean stuff, it doesn't mean it's hard to understand and i added some notes :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerControls : MonoBehaviour {
 
     private GameObject player;  //My Player.
     
     //Each Vector represents a Lane/Path.
     private Vector3 lane1;
     private Vector3 lane2;
     private Vector3 lane3;
     private Vector3 lane4;
 
     void Start()
     {
         //Setting up some references.
         player = this.gameObject;
 
         lane1 = new Vector3(-3.5f, 0, 0);
         lane2 = new Vector3(-1.2f, 0, 0);
         lane3 = new Vector3(1.3f, 0, 0);
         lane4 = new Vector3(3.7f, 0, 0);
     }
 
     void Update()
     {
         if (Input.GetKeyDown("a"))  //If Player pressed A:
         {
             if (player.transform.position == lane2) //If he is on lane 2, then move him to lane 1 (Go left)
             {
                 player.transform.position = lane1;
             }
 
             if (player.transform.position == lane3) //If he is on lane 3, then move him to lane 2 (Go left)
             {
                 player.transform.position = lane2;
             }
 
             if (player.transform.position == lane4) //If he is on lane 4, then move him to lane 3 (Go left)
             {
                 player.transform.position = lane3;
             }
         }
 
         if (Input.GetKeyDown("d")) //If the Player pressed D (Here is where it doesn't work)
         {
             if (player.transform.position == lane1) //If he is on lane 1, then move him to lane 2 (Go Right) BUT INSTEAD, It moves him immediately to 4.
             {
                 player.transform.position = lane2;
             }
 
             if (player.transform.position == lane2) //If he is on lane 2, then move him to lane 3 (Go Right) BUT INSTEAD, It moves him immediately to 4.
             {
                 player.transform.position = lane3;
             }
 
             if (player.transform.position == lane3) //If he is on lane 3, then move him to lane 4 (Go Right) BUT INSTEAD, It moves him immediately to 4.
             {
                 player.transform.position = lane4;
             }
         }
     }
 }

Any help would be appreciated, thanks for reading this far, please reply asap.

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

2 Replies

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

Answer by jwulf · Mar 07, 2017 at 11:39 AM

While I generally think it is a bad idea to compare Vectors with ==, what you observe is quite logical - your code has the following structure (just the part, when D is pressed).

 if(playerIsInLane1) { moveToLane2(); }
 if(playerIsInLane2) { moveToLane3(); }
 if(playerIsInLane3) { moveToLane4(); }

Now imagine the player is in lane 1.

  • The scripts starts at line 1. The condition is true, because the player is in lane 1. => The script moves the player to lane 2.

  • Now the script is in line 2. The condition is true(!) because the player is in lane 2 (player has just been moved there). => The script moves the player to lane 3.

  • Now the script is in line 3. The condition is true(!) because the player is in lane 3 (player has just been moved there). => The script moves the player to lane 4.

That's why the player is transported to lane 4 immediately.

Solution: Either use else if instead of if from the second condition onwards; or use return; after applying the movement of the first true condition. (But not, if you use your Update-Method for more stuff of course.)

Comment
Add comment · Show 3 · 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 Bryangs · Mar 07, 2017 at 03:36 PM 0
Share

Yeah, i get your point. I was thinking in that, but it doesn't make much sense because the LEFT part works fine, so why would the RIGHT part bug? If one works, then both should. I will try the else if thing though.

Works fine! I just wanted to know why does one direction works, and the other doesn't...

avatar image jwulf Bryangs · Mar 07, 2017 at 03:39 PM 0
Share

Well, do the same thing I did with the other side - code when A is pressed

      if(playerIsInLane2) { moveToLane1(); }
      if(playerIsInLane3) { moveToLane2(); }
      if(playerIsInLane4) { moveToLane3(); }

Now imagine your player is in lane 4.

  • The script starts in line 1. The condition is false, so nothing is changed.

  • Now the script is in line 2. The condition is false, so nothing is changed.

  • Now the script is in line 3. The condition is true => player moves to lane 3.

Nothing more happens - in the next Update()-Call, Get$$anonymous$$eyDown("a") is not true, so the script doesn't execute this part again (until d is pressed again) - that's why that part works just fine.

avatar image Bryangs jwulf · Mar 07, 2017 at 05:07 PM 0
Share

Oh, okay, now i understand. I can't believe i didn't realized this before xD Tnks!

avatar image
0

Answer by N1warhead · Mar 07, 2017 at 03:27 AM

Personally what I'd do to avoid complications like this is make it where when you press A or D, to just move player a set distance. Then when you get to the last lane on either side, lock out the key for moving to another lane that don't exist. (this is untested) but no errors on my end.

     bool canMoveRight;
     bool canMoveLeft;
 
     void Update(){
         // Moves player left.
             // May have to switch 5 and -5.
         if (Input.GetKeyDown (KeyCode.A && canMoveLeft)) {
             transform.position = new Vector3 (-5, transform.position.y, transform.position.z);
         }
         // Moves player right.
         if (Input.GetKeyDown (KeyCode.D && canMoveRight)) {
             transform.position = new Vector3 (5, transform.position.y, transform.position.z);
         }
 
         // Check player position, if we reach a max distance (left or right) lock out the key to prevent moving.
         if (transform.position.x == 20) {
             canMoveRight = false;
         } else {
             canMoveRight = true;
         }
         if (transform.position.x == -20) {
             canMoveLeft = false;
         } else {
             canMoveLeft = true;
         }
     }

Comment
Add comment · Show 3 · 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 N1warhead · Mar 07, 2017 at 03:28 AM 0
Share

You may have to mess with the last 2 numbers a bit to get the desired result. But this script should do it.

avatar image Bryangs · Mar 07, 2017 at 03:35 PM 0
Share

I could do that, but i want the player to go SPECIFICALLY from one point to another. If i used this method and for some reason the player moved a little bit as the result of an external force, making him go X in some direction would make him stay on the wrong place.

avatar image jwulf Bryangs · Mar 07, 2017 at 03:45 PM 0
Share

Yes, but in your current situation, if "for some reason the player moved a little bit as the result of an external force" none of the comparisons (position == lane1, position == lane2, etc.) will evaluate to true.

On the whole, you might want to consider using an enum for the positions, so you have some fixed values to compare. But I don't want to annoy you with more comment than you asked for ;)

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

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

Making a bubble level (not a game but work tool) 1 Answer

Coroutine fires once after destroying gameobjects in a scene and recreating new objects 0 Answers

Disabling script while in certain position 1 Answer

Still doing Running Animation? 1 Answer

Jumping from wall to wall Can't jump away from wall to wall 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