- Home /
greater or less than with vector coordinates
How do I compare two Vectors and ask, "if either of the vector points is greater than equivalent points on this other vector" or "if either one of the vector points is greater than this number"?
I could do the check one at a time using x and y but I'm wondering if there's a simpler solution.
// Get the target position in viewport space. The bottom left is (0,0) and the upper right is (1,1)
private var targetViewportPosition : Vector2;
targetViewportPosition = camera.WorldToViewportPoint (target.position);
// Set bounding box limits
private var boundingBottomLeft = Vector2(0.2, 0.2);
private var boundingTopRight = Vector2(0.8, 0.8);
if (boundingCheck
&& (targetViewportPosition < boundingBottomLeft
|| targetViewportPosition > boundingTopRight))
Yeah, I'm pretty sure that one way or another you'll end up having to compare them one at a time. On the other hand, there's no reason why you can't write a function that does that automatically, and just use that as a shortcut (ins$$anonymous$$d of having to write it out every time). It's the wonderful thing about turing-complete languages- if a function that you need doesn't exist, you can just write one!
if (boundingCheck && (targetViewportPosition.x < boundingBottomLeft
|| targetViewportPosition.y < boundingBottomLeft
|| targetViewportPosition.x > boundingTopRight
|| targetViewportPosition.y > boundingTopRight))
That's the best I can do I suppose. I only need to check once but if I do need to check multiple times I'll create a function to do it. Thanks! :)
i'm really a noob, but this is what i had while running some tests : // GUI
function OnGUI ()
{
// Show info
GUI.Label (Rect (10, 10, 150, 25), "pos : " + rigidbody.position);
GUI.Label (Rect (10, 30, 150, 25), "velocity : " + rigidbody.velocity);
GUI.Label (Rect (10, 50, 150, 25), "speed : " + rigidbody.velocity.magnitude);
GUI.Label (Rect (10, 70, 150, 25), "dist : " + dist.magnitude); // dist = player.transform.position - transform.position;
GUI.Label (Rect (10, 90, 150, 25), "ang : " + transform.rotation.eulerAngles);
}
so there may be a way ins$$anonymous$$d of rigidbody's :
http://unity3d.com/support/documentation/ScriptReference/Vector2.html
scroll down to Class Functions.
You could use Rect and the Contains function. Not much of a difference, but better have too many options than not enough isn't it ?
Your answer
Follow this Question
Related Questions
C# Convert Vector3[] to Vector2[] 3 Answers
Create custom constructors for certain Value types/Objects (Vector3, Transform, etc...) 2 Answers
[HELP] Translating vector 2 co-ordinates to real space 1 Answer
Transfer values from C# to Javascript 1 Answer
how to find a point(vector2) between two points(Vector2) of a line (line renderer) 2 Answers