The question is answered, right answer was accepted
How do I change the transform position of my player when it reaches a certain x-position?
I want to make my player's x-position change to a different one when it hits a certain x-position. For example, when my player's x-position is less than -10.0f, I want it to become 10.0f. This is my script so far, but it's not working:
void Update () {
if (transform.position.x < -10.0ff) {
transform.position = new Vector3 (10.0f, transform.position.y, transform.position.z);
}
if (transform.position.x > 10.0ff) {
transform.position = new Vector3 (-10.01f, transform.position.y, transform.position.z);
}
}
Answer by FuzzyLogic · Feb 26, 2018 at 11:56 PM
Unity should be giving you an error about "unexpected symbol 'f'" because of lines 4 and 7 in your sample code. Otherwise, it should work, although it would be better to have if/else if instead of two separate if branches since they are exclusive.
void Update () {
if (transform.position.x < -10.0f) {
transform.position = new Vector3 (10.0f, transform.position.y, transform.position.z);
} else if (transform.position.x > 10.0f) {
transform.position = new Vector3 (-10.01f, transform.position.y, transform.position.z);
}
}
It's not an error but you probably want to add/subtract 20.0f to/from position.x instead of just setting x to min/max. Consider if position.x is -10.5f. When you wrap around, you generally want to retain the remainder beyond -10.0f (-0.5f) so your new position would be 9.5f instead of 10.0f. Otherwise your object may appear to stall when it wraps around, moving only a fraction of the distance that was intended.
void Update () {
if (transform.position.x < -10.0f) {
transform.position = new Vector3 (transform.position.x + 20.0f, transform.position.y, transform.position.z);
} else if (transform.position.x > 10.0f) {
transform.position = new Vector3 (transform.position.x - 20.0f, transform.position.y, transform.position.z);
}
}
Answer by Chik3r · Feb 26, 2018 at 11:41 PM
Hello, @S_jay1 Replace :
if (transform.position.x < -10.0ff) {
transform.position = new Vector3 (10.0f, transform.position.y, transform.position.z);
}
if (transform.position.x > 10.0ff) {
transform.position = new Vector3 (-10.01f, transform.position.y, transform.position.z);
}
With :
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -10f, 10f), transform.position.y, transform.position.z);
What Mathf.Clamp does?
Mathf.Clamp will check if a value is less than the minimum(-10f here) and return the minimum if its lower, and check if a value is higher tha the maximum(10f here) and return the maximum if its higher.
This is not the same behaviour. The op wants wrap, not clamp.
For some reason, this script isn't working. The player is not moving back to the -10 position. It is just constantly moving forward. Is there something wrong with my script?
void Update () {
GetComponent<Rigidbody> ().velocity = new Vector3 (2, 0, 0);
//I'm using this just to move the player foward
transform.position = new Vector3($$anonymous$$athf.Clamp(transform.position.x, -10f, 10f), transform.position.y, transform.position.z);
}
I didn't even realize that the player was attached to a Canvas. This caused the script to not work at all. The clamp freezes my player to a certain position, I want to loop back. Thank you for showing me how a clamp works though!
Follow this Question
Related Questions
smooth Movement script for Player prefab in Steam VR using Oculus Rift touch controllers? 1 Answer
[C#] [2D] Not changing positions correctly 1 Answer
Player Can Not get off platform 1 Answer
Triggering Video Player with OSC (UniOSC and QLab),Trigger Video Player with OSC (UniOSC and QLab) 1 Answer
How do I get a character to walk on walls and ceilings? 1 Answer