- Home /
Vector3 Clamp
I tried limiting a Vector3 with Mathf.Clamp, but it caused a jittery effect on the gameobject.
public var moveSpeed : float;
public var minimumX : float;
public var maximumX : float;
public var minimumY : float;
public var maximumY : float;
public var resetTimerX : float;
public var resetTimerY : float;
public var defaultPos : Vector3;
public var newPos : Vector3;
function Start() {
defaultPos = gameObject.transform.localPosition;
newPos = gameObject.transform.localPosition;
}
function Update() {
newPos.x += Input.GetAxis("Mouse X") * moveSpeed * Time.deltaTime;
newPos.y += Input.GetAxis("Mouse Y") * moveSpeed * Time.deltaTime;
gameObject.transform.localPosition = newPos;
newPos.x = Mathf.Clamp(newPos.x, minimumX, maximumX);
newPos.y = Mathf.Clamp(newPos.y, minimumY, maximumY);
}
I found that it's the clamp causing this. Can vector3's not be clamped? What other meathods can be used to limit position?
Answer by Bunny83 · Dec 31, 2012 at 04:02 PM
Sure there's a jitter since you clamp after you assigned the values...
So the change that happens in the current frame is always transferred to the objects position. You have to clamp before you assign the position to the object.
function Update()
{
newPos.x += Input.GetAxis("Mouse X") * moveSpeed * Time.deltaTime;
newPos.y += Input.GetAxis("Mouse Y") * moveSpeed * Time.deltaTime;
newPos.x = Mathf.Clamp(newPos.x, minimumX, maximumX);
newPos.y = Mathf.Clamp(newPos.y, minimumY, maximumY);
gameObject.transform.localPosition = newPos;
}
$$anonymous$$eep in $$anonymous$$d that newPos is a Vector3 so it contains also a z value which is never changed but always assigned. You can not change the z value from another script unless you update newPos every frame:
newPos = gameObject.transform.localPosition;
// apply your changes to newPos and clamp it afterwards.
gameObject.transform.localPosition = newPos;
The amazing things that I can do when I think :) thanks man so much for the help!
Answer by alexfeature · Dec 31, 2012 at 07:25 AM
Hey Corey,
The reason for this is probably coming form interference of other scripts on the position of this object.
Make sure that movement of this object is handled only from one script, and preferably one function.
Also, keep in mind that if you make the object move with physics (ie have a rigid body and apply force etc.) Messing with that object's movement in scripts will cause jitters.
This is because the physics engine is trying to move it at the same time as your are.
Hope this helps, Alex
You're right in saying that I have another script controlling the object's movement, but it's a long a different axis. After none of that worked, I tried controlling the object's localPosition directly rather than positioning it by a Vector3 and it worked.
Cool. Problem solved.
You should mark the question as answered then.
Well yeah haha, but it's more complicated than it has to be that way.
Answer by RJ45 · Aug 20, 2016 at 08:53 AM
/// /// Simple helper to clamp a range to vector3 /// public class Vector3Range { public Vector3 Min; public Vector3 Max;
/// <summary>
/// Clamp a range
/// </summary>
public Vector3 Clamp(ref Vector3 value)
{
value.x = Mathf.Clamp(value.x, Min.x, Max.x);
value.y = Mathf.Clamp(value.y, Min.y, Max.y);
value.z = Mathf.Clamp(value.z, Min.z, Max.z);
return value;
}
}
For most usages you would want a simple static with min and max as parameters rather than a class but I had a use.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090148im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
object transform and clamp in local direction 0 Answers
Limit Camera movement by distance from target 1 Answer
How to limit a movement vector without affecting gravity 2 Answers
Clamping Vector3 2 Answers
Clamp X position of object 1 Answer