Vector3.magnitude و Vector3.normalized در یونیتی
gameover.blog.ir
فرض کنید ما دو نقطه در فضای سه بعدی داریم(یعنی هر کدومشون xyz دارن)
میایم و این نقطه رو از هم تفریق می کنیم.
یعنی چه؟
یعنی اینکه x2-x1,y2-y1,z2-z1 هر کدوم رو به ترتیب از هم کم می کنیمT،یک نقطه دیگه deltax,deltay,deltaz بدست میاد .
حالا اگه این deltax,deltay,deltaz رو به توان 2 برسونیم و با هم جمع کنیم و از نتیجش جذر بگیریم فاصله ی بین دو نقطه بدست می آد.
همچنین اگه نقطه ی
deltax,deltay,deltaz (این سه تا در یک delta خلاصه یا کپسوله یا پهان
سازی میشه) رو نرمالیزه یا normalized کنیم بردار جهت یا حرکت یا direction
بدست می یاد.
مثال: (به توضیحات انگلیسی دقت شود)
Vector3 pos1; // start position Vector3 pos2; // click position Vector3 moveDistance = 3.0f; // move 3 units in the calculated direction Vector3 delta = (pos2 - pos1); // delta is the vector difference between pos1 and pos2 float length = delta.magnitude; // distance in a straight line from pos1 to pos2 // direction is delta scaled to a normalized (unit) vector // A unit vector has all components reduced so that it has a length of 1 Vector3 direction = delta.normalized; direction.z = 0.0f; // don't move in z direction // move the object from pos1 towards pos2 by the desired distance pos1 += direction * moveDistance;نکته:برای محاسبه فاصله بین دو بردار(vector) از دستور Vector3.Distance هم استفاده میشه.
مثال :
a=new Vector3(5,-10,100);
b=new Vector3(6,8,1);
Vector3.Distance(a,b);
منبع:
http://farside.ph.utexas.edu/teaching/301/lectures/node28.html
http://gamedev.stackexchange.com/questions/106269/unity-determine-direction-vector3-from-a-point-in-space-and-an-angle-in-eulers
بیشتر توضیح بدین magnitude چی پس...