- Home /
Vector2.magnitude and Touch.deltaPosition understanding....
I am curious what a vector2 is made up of. I know it has an x and y component, but does it have a direction component as well? I would assume not as it just stores the coordinates of a location.
If the vector2 stores only x,y components, why does it have a magnitude? Does that method actually figure out the hypotenuse formed form the two side lengths x and y in a triangle?
Why is this useful except at origin? Wouldn't you mainly use triangles between separate vector2 objects?
My second question is about Touch("an index").deltaPosition.
When referencing where a touch was previously to find out direction it has moved you use the expression:
(Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition)
to find out where it was last update. Do you ALWAYS subtract? But then that means deltaPosition has to be negative sometimes. When is it negative?
How would i also set up debugging or a simple way to see variables at runtime for an android application to check this? (preferably without using a LAN as my only option is AdHoc and android (even with an AdHoc hack to enable it) won't connect to my pc. How would i make a counter that is displayed on screen to keep track of variables then?
Answer by Eric5h5 · Nov 20, 2011 at 03:16 AM
A Vector2 is two floats, and magnitude is simply the square root of x*x + y*y
. Presumably it looks something like this (simplified):
struct Vector2 {
public float x;
public float y;
public float magnitude {
get {return Mathf.Sqrt (x*x+y*y);}
}
public Vector2 (float x, float y) {
this.x = x;
this.y = y;
}
}
x and y are whatever you say they are. They can be coordinates, or a direction, or something else. As for the rest, you should really just ask one question per question.
Answer by aldonaletto · Nov 20, 2011 at 04:25 AM
@Eric5h5 is right: you should post 2 more questions.
1- Let me answer the second question: deltaPosition is the touch displacement since the last change, and its polarity and value depend on the direction of movement. You must always subtract deltaPosition from GetTouch[n].position to find the previous position.
2- Answering the 3rd quetion: you can use OnGUI to show some variable of interest:
var speed: float; // <- you are interested in this variable
function OnGUI(){ // show the variable in a GUI.Label: GUI.Label(Rect(10,10,320,50),"Speed:"+speed.ToString()); }
What do you mean by displacement, is that just a magnitude or does it have a negative value sometimes? What occasions would it? Any examples of values and situations for deltaPosition? The documentation doesnt shiw cases and values at all.
I thought i would save time by combining into one. Guess ill have to post more then.
How would i do that using texture gui? I heard ongui is terrible for mobile devices do to constant gui element calls.
This displacement is a Vector2: its x and y components hold the difference between the current and the last positions. If the touch is at [50,50], for instance, and you move the finger to [20,70] - what I suppose is to left/up - deltaPosition will be [-30,20] (result of current point [20,70] $$anonymous$$us old point [50,50]).
About the GUI: I suppose OnGUI is more expensive than GUIText, since it's called several times during each Update. You can use a GUIText for debugging:
var gText: GUIText; // drag the GUIText here from the hierarchy
function Update(){ var debug: String; debug += var1+"\n"; // add the variables you want to debug += var2+"\n"; // watch with a new line at the end debug += var3+"\n"; // to have one variable per line ... gText.text = debug; // show the string created }