- Home /
Question by
Krueger · Dec 05, 2014 at 06:41 PM ·
playercontrollerlogicrestrictmovement
If player is restricted on a value, why does it not "snap" back?
I used a Vector2 player restriction script that I changed to Vector3. In doing so I came across an issue in the corners of the game space. You can continue to increase and decrease on the X axis past the restriction.
float currPosX = transform.position.x;
float currPosY = transform.position.y;
float currPosZ = transform.position.z;
if (transform.position.x <= -10)
{
transform.position = new Vector3(-10f, currPosY, currPosZ);
}
else if (transform.position.x >= 10)
{
transform.position = new Vector3(10f, currPosY, currPosZ);
}
else if (transform.position.z <= -10)
{
transform.position = new Vector3(currPosX , currPosY, -10f);
}
else if (transform.position.z >= 10)
{
transform.position = new Vector3(currPosX , currPosY, 10f);
}
also tried to alter using && || statements based off other axis, probably logic error though.
Comment
Answer by jenci1990 · Dec 05, 2014 at 07:20 PM
Try this:
float currPosX = transform.position.x;
float currPosY = transform.position.y;
float currPosZ = transform.position.z;
currPosX = Mathf.Clamp(currPosX,-10f,10f);
currPosZ = Mathf.Clamp(currPosZ, -10f, 10f);
transform.position = new Vector3(currPosX, currPosY, currPosZ);
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Player 1 and Player 2 move together 1 Answer
How do I set up my players controller script (How do I change the controls used to move and look) 2 Answers
Get player pulled to position/object 1 Answer
Character fall animation 1 Answer