- Home /
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.
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.)
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...
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.
Oh, okay, now i understand. I can't believe i didn't realized this before xD Tnks!
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;
}
}
You may have to mess with the last 2 numbers a bit to get the desired result. But this script should do it.
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.
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
![](https://koobas.hobune.stream/wayback/20220612112620im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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