- Home /
how to record the Vector3 position on condition?
I have implemented a touch and drag on GameObject on Iphone/Android. I am using if statements and "OR" gates for detecting gameObject.transform.position for each axis and when any condition is met then i am setting the gameObject transform position to Vector3.zero.
But what i want is to avoid the gameObject from crossing the Boundary Conditions. I don't want to use localPosition since the Object needs to remain independent in hierarchy.
the code that I am using is :
var object : GameObject;
function LateUpdate()
{
if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); }
var theTouch : Touch = Input.GetTouch (0);
var ray = Camera.main.ScreenPointToRay(theTouch.position);
var hit : RaycastHit;
if (Physics.Raycast (ray))
{
gameObject.animation.Play();
dragmethod();
}
}
function dragmethod() { //var targetlimit =imageTarget.transform.position; //var target = object.transform.position; //var limit = new Vector3(0,0,0);
for (var touch : Touch in Input.touches)
{
if(touch.phase == TouchPhase.Moved && Input.touchCount==1)
{
var cameraTransform = Camera.main.transform.InverseTransformPoint(0, 0, 0); var touchPosition = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, cameraTransform.z )); var origin : Vector3;
object.transform.position = touchPosition; origin = Vector3 (0,0,0);
if(object.transform.position.x > 0.50 ||object.transform.position.x < -0.50 ||object.transform.position.y > 0.50 || object.transform.position.y < -0.50 || object.transform.position.z > 0.50 || object.transform.position.z < -0.50)
{ object.transform.position = new Vector3 (0,0,0);
} } } }
Is there any way to limit the gameObject inside a "Virtual Boundary" without making it a child of any other Object?
Answer by fafase · Jul 23, 2012 at 04:34 PM
Somehow maybe:
if(transform.position.x < minX)transform.position.x=minX;
else if(transform.position.x > maxX)transform.position.X=maxX;
if(transform.position.y < minY)transform.position.y=minY;
else if(transform.position.y > maxX)transform.position.y=maxY;
if(transform.position.z < minZ)transform.position.z =minZ;
else if(transform.position.z > maxZ)transform.position.z =maxZ;
If your position exceeds the boudaries, the position is the bounadaries.
In my code the transform.position is following the touchPosition // var touchPosition = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, cameraTransform.z ))
Thus the gameObject is following a Vector3 value am i right? Your code suggests to place the gameObject at specific values of $$anonymous$$axX $$anonymous$$axY and $$anonymous$$axZ(3 dimensions are must coz I am implementing this in Augmented Reality)...
Sorry if I am wrong but please tolerate my lack of knowledge.Is there any other way,I think I am implementing it the wrong way.
ok, you pass the value of the touch to the transform so you have a vector3 for instance (3,11,14). Your object is about to be positioned at this position but in the same frame before rendering you apply:
if(transform.position.x < $$anonymous$$X)transform.position.x=$$anonymous$$X;
else if(transform.position.x > maxX)transform.position.x=maxX;
if(transform.position.y < $$anonymous$$Y)transform.position.y=$$anonymous$$Y;
else if(transform.position.y > maxX)transform.position.y=maxY;
if(transform.position.z < $$anonymous$$Z)transform.position.z=$$anonymous$$Y;
else if(transform.position.y > maxX)transform.position.z=maxY;
Let's say all $$anonymous$$ are 0 all max are 10. As a result your new vector will be (3,10,10) and your object is inbound.