may be I need to review some math… but… I don’t quite get, If I have 2 vectors in 3D, and I use Vector3.angle… why … do I get only one angle… I should get two angles unless the function returs the angle in the plane formed by the 2 vectors.
Hi. From the Unity Scripting Reference, i found that:
Vector3.Angle static function Angle (from : Vector3, to : Vector3) : float Description Returns the angle in degrees between from and to.
var target : Transform; function Update () { var targetDir = target.position - transform.position; var forward = transform.forward; var angle = Vector3.Angle(targetDir, forward); if (angle < 5.0) print(“close”); }
I think it works perfect. Is the angle between 2 vectors. Which 2 angles do u want to know?
this is my aim: I am mapping the Touch onto spherical coordinates. So, if I ray cast each Touch (in pixel coordinate) I have a Ray (so a vector in 3D). The first touch represents the first ray, when I move my finger, any further movement will represent my second ray. So, I want to use that angle as a rotation angle of my object.
Knowing the initial and the following positions of my Touch(es), I know the angle… but I can’t do that with just one angle… I guess. What am I missing? …
yes, when I move the finger my touch is in TouchPhase.Moved so I can always grab the deltaPosition and the new position… which gives me the direction… no? but how do I apply it to the rotation?
you can actually cast 2 rays on the sphere, and make sure one of them stays where the first touch was, then make one of the 2 remaining keep it’s x or z or whatever coord you have on the horizontal, the same as the first ray, and the other one of the 2 make it keep it’s other coord.
so you will have your 2 angles. so you will know how much you will rotate on xx and z or whatever
or when you will use Vector3.Angle(vector1,vector2); make sure you edit the 2nd vector coords to match the coord of the first, so they will form the plane on the horizontal.
edit:
since the Vector3.Angle will return the angle in the plane formed by the 2 vectors.
Uhm. I am actually trying to do that… and the Vector1, is my first touch… Vector2 is actually the following position of the finger on the screen… but I keep the Vector1 saved.
But what shall I use in the gameObject.transform.Rotate(xxx,xxx,xxx)?
If you use the code I posted, it should work. You don’t need to use .Rotate if you already solved for the rotation, you can just set it manually like I did (transform.rotation = whatever).
Why not just rotate based on the touch screen delta?
from:
// Performs a mouse look.
var horizontalSpeed : float = 2.0;
var verticalSpeed : float = 2.0;
function Update () {
// Get the mouse delta. This is not in the range -1...1
var h : float = horizontalSpeed * Input.GetAxis ("Mouse X");
var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");
transform.Rotate (v, h, 0);
}