Hi, all,
I would like to modify the following script, constraining movement to a defined perimeter
ie. 1200 x 1200 area. Any suggestions?
Thanks.
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
}
}
}
untested, but the idea is something like this:
var v = object.transform.position - swipeVector;
//now constrain v:
v.x = Math.Max(v.x,-1200);
v.x = Math.Min(v.x, 1200);
v.y = Math.Max(v.y,-1200);
v.y = Math.Min(v.y, 1200);
// and apply the newly constrained position to the transform
object.transform.position = v;
Bye, Lucas
Thanks for the code Lucas, I appreciate your time!
In implementing your script I noticed that the debugger was unhappy with the Math function and would not work. I tried Mathf instead and got a strange result- with the touch movement drifting, but not constrained. Would you have any other suggestions?
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
var v = object.transform.position - swipeVector;
//now constrain v:
v.x = Mathf.Max(v.x,-120);
v.x = Mathf.Min(v.x, 120);
v.y = Mathf.Max(v.y,-120);
v.y = Mathf.Min(v.y, 120);
// and apply the newly constrained position to the transform
object.transform.position = v;
}
}
}