- Home /
Why is Clamp changing objects starting position ?
I have a simple line of code to move an object left and right using the a, d keys, which works perfectly - public class PlayerMovement : MonoBehaviour { public int speed = 0;
void Update()
{
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * speed, 0.0f, 0.0f);
}
}
However, when I try to restrict the objects movement using Clamp it changes the objects starting position. The player/object is set to origin (0,0,0) but when the game starts its position is now whatever value Ive set maxX to in the inspector. Its always starting to the left so If I assign maxX as 10 its starting position is now -10 from origin. The game is 3d and there are no animations. I'm not getting any errors, but I don't understand why the object no longer starts at origin?
public class PlayerMovement : MonoBehaviour
{
public int speed = 0;
public float maxX;
void Update()
{
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * speed, 0.0f, 0.0f);
Vector3 pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -maxX, maxX);
transform.position = pos;
}
}
Any Ideas???
Answer by Bunny83 · Apr 15, 2018 at 10:48 AM
What do you mean by "reset"? You restrict the movement to a world space position range of -maxX to +maxX. What value have you maxX set to and what position does your player have in the editor and what position do you get when you start the game? My guess is that your player is already outside your specified range and it get of course moved into the range once the clamp runs.
You should include your numbers in the question. Maybe a screenshot? Is it a 2d or 3d game? Where is the camera located and where does it look at? Does your player have an animation / animator component? Maybe you have an empty animation with one frame at the 0,0,0 position?
We only see what you show us and what we know at the moment is not enough to answer this question. We can only guess at this point.
Sorry, badly worded explaination. Changed title and description hopefully more helpful
Are you sure about your position? Try adding this in Start:
void Start()
{
Debug.Log("Start position: " + transform.position);
Debug.Log("maxX: " + maxX);
}
If your object is a child object keep in $$anonymous$$d that the position shown in the inspector is the localPosition, not the world space position. I'm still quite sure that your object is not really in the worldspace range of -10 to 10 at start but most likely more to the left.
If that's not the case you may have another script that tinkers with the position of your player.
I should try more sleep and less caffeine, I can see where my explanation is confusing you, sorry.
$$anonymous$$y terrain is a 70x70 square, my player object is set (at Start) in position +35 on the X axis or exactly hallway across the board. (I've been thinking as this of centre.)
Your Debug.Log shows "Start position = 35.0, 2.0, 2.0)" and "maxX = 10"
At game start this is placing my game object at -10 on the X axis and it allows me to travel another 10 to the left and nothing to the right