参考教程

http://coder.beitown.com/archives/1279

https://blog.csdn.net/renkai0406/article/details/63800248?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&dist_request_id=1619527371618_58607&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control

1.移动的几种方式:

​ transform.Translate(Vector3); //移动到目标点,可使用 Time.delta * Vector3.normalized 逐贞 匀速 移动过去,

transform.position = Vector3;

2.旋转的几种方式

transform.rotation = Qua;

transform.Rotate();

    var rt = Vector3.RotateTowards(transform.forward, target.transform.position, Time.deltaTime, 0);
    transform.rotation = Quaternion.LookRotation(rt);
    transform.position = Vector3.MoveTowards(transform.position, target.transform.position, Time.deltaTime * speed);

Vector3常用API.

1.Vector3.ClampMagnitude(Vector3 vector,float maxLength)锁定最大坐标。

用途:限制某个物体的可移动范围, 比如 老夫子大招捆人,只能在半径范围内移动。

官方示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using UnityEngine;
using System.Collections;public class ExampleClass : MonoBehaviour
{
// Move the object around with the arrow keys but confine it
// to a given radius around a center point.
public Vector3 centerPt; //中心点位置,类似于老夫子捆人时候灯塔的坐标
public float radius; //最大移动半径,
void Update()
{
//Input.GetAxis("Horizontal") 按下 左右方向键的的值。:比如朝着指定方向键方向移动
// Get the new position for the object.
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Vector3 newPos = transform.position + movement;
// Calculate the distance of the new position from the center point. Keep the direction
// the same but clamp the length to the specified radius.
Vector3 offset = newPos - centerPt;
transform.position = centerPt + Vector3.ClampMagnitude(offset, radius);
}
}
    var v100 = new Vector2(1, 1);
    var v101 = new Vector2(2, 0);
    var dotValue = Vector2.Dot(v100, v101);
    var angle = Vector2.Angle(v100, v101);
    var dotValue2 = v100.magnitude * v101.magnitude * Mathf.Cos(angle * Mathf.Deg2Rad);
    var cosValue = dotValue / (v100.magnitude * v101.magnitude);
    var angleByCosValue = Mathf.Acos(cosValue) * Mathf.Rad2Deg;
    Debug.Log(" angle by cos value  : " + angleByCosValue);
    Debug.Log(" v1 v2 之间的cos 值 :" + cosValue + "  " + Mathf.Cos(angle * Mathf.Deg2Rad));
    Debug.Log(">>>>>>>>> Vector.Dot : " + dotValue + " angle : " + angle + " dotValue2: " + dotValue2);