- Home /
changing x multiplies x,y and z position for no reason
every time I press LeftArrow or RightArrow, the x,y and z axis multiply by themselves. (1,2,4,8), but I don't know what causes it. The x pos value also gets very strange, going to values like -3 while that shouldn't be possible. Any idea?
using UnityEngine;
public class MoveScript : MonoBehaviour {
[SerializeField] private float activeLane = 0; //Left lane = -1, Middle lane = 0, Right lane = 1.
public GameObject player;
private Vector3 playerTf;
[SerializeField] private float playerSpeed = 1;
void Start () {}
void Update () {
player.transform.Translate(transform.forward * playerSpeed);
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
if (activeLane >= -1 && activeLane <= 1)
{
activeLane--;
player.transform.Translate(activeLane,player.transform.position.y,player.transform.position.z);
}
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
if (activeLane >= -1 && activeLane <= 1)
{
activeLane++;
player.transform.Translate(activeLane, player.transform.position.y, player.transform.position.z);
}
}
}
}
Answer by Tobychappell · Jul 08, 2018 at 12:18 PM
Transform.Translate moves the transform BY a given Vector3 or XYZ. it desnt SET the position.
Link: https://docs.unity3d.com/ScriptReference/Transform.Translate.html
Consider just setting the position directly.
so if your y value was 1 it would add 1, ehich becomes 2, added again, equals 4 etc, this is why it is doubling.
Answer by Bunny83 · Jul 08, 2018 at 12:41 PM
Toby is right so make sure you give an upvote ^^. Though you can actually simplify a lot of your code by moving the lane switching into a seperate method.
Also note that you currently move forward by a fix amount each frame. This makes your game framerate dependent. So if a user has 30 fps and another has 300 fps the game runs 10 times as fast. You should multiply your movement vector by Time.deltaTime to make it time dependent. So speed no longer specifies the amount you want to move per frame, but intstead you specify the units per second. This will give you the same speed regardless of the framerate.
void SwitchLane(int aNewLane)
{
activeLane = Mathf.Clamp(aNewLane, -2, 2);
Vector3 tmp = transform.position;
tmp.x = activeLane;
transform.position = tmp;
}
void Update()
{
player.transform.Translate(transform.forward * playerSpeed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
SwitchLane(activeLane - 1);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
SwitchLane(activeLane + 1);
}
}
Finally your if conditions didn't make too much sense. When you press left you only need to check the left border and when you press right you only need to check the right border. Your double conditions, given your lane switching actually works, will make the player get stuck either at -2 or +2. That is because if the player is at "-1" and you press left again by your conditions you are allowed to move once more. That means you end up at -2. However now neither the left or right button will ever do anything since you're position is smaller than -1.
I've assumed you want 5 lanes from your conditions (-2, -1, 0, 1, 2). That's why i used Clamp with a lower limit of -2 and an upper limit of 2. However if you want 3 lanes (-1, 0, 1) you have to use -1 and 1 of course.