1

I have two points (cube game object) in Unity, P1(x,y,z) and P2(x,y,z) and I have to set the MainCamera position and rotation along the vector between P2P1.

I tried different methods but was not successful. All suggestions are welcomed. Thanks.

Majid Nasirinejad
  • 107
  • 1
  • 2
  • 13
  • Please be more specific: what do you mean by "set the MainCamera position and rotation along the vector between P2P1"? Is that the direction? Are there any requirement concerning the roll of the camera? – Emond Jun 21 '15 at 14:45
  • 1
    research "camera lookat()" –  Jun 21 '15 at 15:14
  • Two points do not span an angle. Are you referring to the angle formed by P1, the origin, and P2? – njuffa Jun 21 '15 at 15:45
  • @njuffa _"Two points do not span an angle"_ - well no. A _logical_ 3D _"point"_ in Unity is a physical `Vector3` (there is no Point3 type) and Unity defines [Vector3.Dot()](http://docs.unity3d.com/ScriptReference/Vector3.Dot.html) for calculating the cosine between them. The OP's **P1** is essentially a vector from (0,0,0) to _"(x,y,z)"_ –  Jun 22 '15 at 07:00
  • @MickyD Camera lookat() was very good point. Thanks – Majid Nasirinejad Aug 10 '16 at 16:46

1 Answers1

5

We can find angle between 2 vectors according the dot production.
angle = arccos ( a * b / |a| * |b| );
where:
a * b = ax * bx + ay * by + az * bz
|a| = sqrt( ax * ax + ay * ay + az * az )
|b| = sqrt( bx * bx + by * by + bz * bz )
Or just use this method: http://docs.unity3d.com/ScriptReference/Vector3.Angle.html

arturx64
  • 943
  • 4
  • 12