I can’t figure out why the following code is not constraining my camera movement properly. When moved via touch, the camera stops at the correct point, but allows the user to drag beyond the boundary. I am looking for it to dead-stop at the end of its perimeter, which is currently set at 5.
Any thoughts?
Thanks,
Greg
var object : GameObject;
var speed = 5.0;
var swipeVector = Vector2.zero;
function Update () {
if(iPhoneInput.touchCount > 0) {
var touch = iPhoneInput.GetTouch(0);
swipeVector = touch.positionDelta * Time.deltaTime * speed;
object.transform.position -= swipeVector;
} else {
if (swipeVector.magnitude > 0.0) {
// gradually reduce the swipe vector in some way
// apply swipe vector
// if swipe vector is 'small enough' set it to zero
if (transform.position.magnitude > 5) {
transform.position = transform.position.normalized * 5;
}
}
}
}
I haven’t done any iPhone stuff before, so I can’t be 100% sure, but the main thing that jumps out at me is you are normalizing a position. Normalizing a Vector is usually done on directional vectors, whereas a Vector3 when used as a position generally represents a point in space.
So if your position is Vector3(200, 200, 200), then by normalizing it you are making it’s magnitude 1, and changing it from a point in space to a direction from zero towards that point in space, and each of the xyz components will be less than one.
So unless you are doing something tricky with the position by placing it around zero, the code seems really odd to me.
Thanks, guys! I reworked the above code (took out some nonsense) and am using the following. It is working well, except the camera movement is more circular around the constraint than square. Anyone know why?
Thanks,
Greg
var object : GameObject;
var speed = 5.0;
var swipeVector = Vector2.zero;
function Update () {
if(iPhoneInput.touchCount > 0) {
var touch = iPhoneInput.GetTouch(0);
swipeVector = touch.positionDelta * Time.deltaTime * speed;
object.transform.position -= swipeVector;
}
if (transform.position.magnitude > 2) {
transform.position = transform.position.normalized * 2;
}
}
I’m having great success with the following code, except for the lower left corner (-x, -y) which still seems to be move in an arc as I move the camera toward the boundary. I can’t understand why the other three quadrants behave normally, and not the -x, -y constraint.
var object : GameObject;
var speed = 5.0;
var swipeVector = Vector2.zero;
function Update () {
if(iPhoneInput.touchCount > 0) {
var touch = iPhoneInput.GetTouch(0);
swipeVector = touch.positionDelta * Time.deltaTime * speed;
object.transform.position -= swipeVector;
}
if(transform.position.x > .5)
transform.position.x = .5;
if(transform.position.y > .5)
transform.position.y = .5;
if (transform.position.magnitude > 2) {
transform.position = transform.position.normalized * 2;
}
}
This is what causes the camera to have circular constraints! Normalizing positions makes very little mathematical sense in general, but in this specific case if effectively gives you the direction from the origin to the camera. You then move the camera to a position 5 units in that direction.
So, if you camera is at (4, 0, 4) then the magnitude of the position will be 5.65 (the square root of 44 + 00 + 4*4). Hence, your constraint kicks in and normalizes the position to (0.707, 0, 0.707) , which you then multiply by 5 to end up at (3.53, 0, 3.53). I hope you’ll agree with me that this isn’t what you want at all?
Now, if you want to constrain your camera to a rectangular area in the horizontal (xz) plane then all you need to do is what ivkoni already suggested:
if (transform.position.x > 5) transform.position.x = 5;
etc.
You can even do it a little more elegantly, using Mathf.Clamp() like so:
Jim, thanks, that makes a lot of sense! I figured that the normalized
variable was probably wrong, but wasn’t sure what else I should use. After studying
what you wrote, I think I have a much better understanding of constraining- especially with mathf clamp.
Thanks again for your (and ivkoni’s) time and the valuable information!