Unity 已知三点 求 三点之间夹角角度

  public double Angle(Vector3 cen, Vector3 first, Vector3 second)
    {
        double M_PI = 3.1415926535897;

        double ma_x = first.x - cen.x;
        double ma_y = first.y - cen.y;
        double ma_z = first.z - cen.z;
        double mb_x = second.x - cen.x;
        double mb_y = second.y - cen.y;
        double mb_z = second.z - cen.z;
        double v1 = (ma_x * mb_x) + (ma_y * mb_y) + (ma_z * mb_z);
        double ma_val = Math.Sqrt(ma_x * ma_x + ma_y * ma_y + ma_z * ma_z);
        double mb_val = Math.Sqrt(mb_x * mb_x + mb_y * mb_y + mb_z * mb_z);
        double cosM = v1 / (ma_val * mb_val);
        double angleAMB = Math.Acos(cosM) * 180 / M_PI;

        return angleAMB;
    }
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Unity 中实现三点随机移动,可以通过以下代码实现: ```csharp public class RandomMovement : MonoBehaviour { public Transform[] waypoints; // 三个随机移动点 public float moveSpeed = 5f; // 移动速 private int currentWaypoint = 0; // 当前移动点 void Start() { // 初始化当前移动点为第一个点 transform.position = waypoints[currentWaypoint].position; } void Update() { // 沿着当前移动点和下一个移动点之间的方向移动 Vector3 direction = waypoints[currentWaypoint + 1].position - transform.position; transform.Translate(direction.normalized * moveSpeed * Time.deltaTime, Space.World); // 如果到达下一个移动点,将当前移动点更改为下一个点 if (Vector3.Distance(transform.position, waypoints[currentWaypoint + 1].position) < 0.1f) { currentWaypoint++; if (currentWaypoint >= waypoints.Length - 1) { currentWaypoint = 0; } } } } ``` 在该代码中,我们首先定义了一个 `waypoints` 数组,包含三个随机移动点。然后我们定义了移动速 `moveSpeed`,并在 `Start` 函数中将当前位置设置为第一个移动点的位置。在 `Update` 函数中,我们计算出当前位置和下一个移动点之间的方向,并使用 `Translate` 方法沿着该方向移动。如果到达下一个移动点,我们将当前移动点更改为下一个点,如果当前移动点已经是最后一个点,则将其更改为第一个点。 你可以将此脚本添加到需要进行三点随机移动的游戏对象上,并将 `waypoints` 数组设置为游戏场景中的三个随机位置。然后该游戏对象将会沿着这三个之间随机移动。 ### 回答2: 在Unity中,可以通过以下代码实现三点随机移动: 1. 首先,在Unity场景中创建三个空物体作为移动点,分别命名为point1、point2和point3。 2. 在脚本中声明一个公有的Transform变量,用于存储当前物体的Transform组件,代码如下: ```C# public Transform objectTransform; ``` 3. 在Start函数中获取当前物体的Transform组件,代码如下: ```C# private void Start() { // 获取当前物体的Transform组件 objectTransform = GetComponent<Transform>(); } ``` 4. 在Update函数中,使用Random.Range函数生成随机的三个坐标值,并将物体移动到对应的位置,代码如下: ```C# private void Update() { // 生成随机的三个坐标值 float randomX = Random.Range(-10f, 10f); float randomY = Random.Range(-10f, 10f); float randomZ = Random.Range(-10f, 10f); // 将物体移动到对应的位置 if (objectTransform != null) { objectTransform.position = new Vector3(randomX, randomY, randomZ); } } ``` 通过以上代码,物体将会在每一帧更新时,随机移动到三个点之一的位置。注意,此代码仅仅是使用三个点来演示随机移动,实际使用中,可以根据具体需设置更多的移动点。 ### 回答3: 在Unity中实现三点随机移动,可以使用以下代码: ```csharp public class RandomMovement : MonoBehaviour { public Transform target1; // 第一个目标点 public Transform target2; // 第二个目标点 public Transform target3; // 第三个目标点 public float speed = 5f; // 移动速 private Transform currentTarget; // 当前目标点 void Start() { // 随机选择一个初始目标点 int randomIndex = Random.Range(0, 3); if (randomIndex == 0) currentTarget = target1; else if (randomIndex == 1) currentTarget = target2; else currentTarget = target3; } void Update() { // 移动向目标点 transform.position = Vector3.MoveTowards(transform.position, currentTarget.position, speed * Time.deltaTime); // 如果到达目标点,则随机选择下一个目标点 if (transform.position == currentTarget.position) { int randomIndex = Random.Range(0, 3); if (randomIndex == 0) currentTarget = target1; else if (randomIndex == 1) currentTarget = target2; else currentTarget = target3; } } } ``` 使用此脚本前,需要在Unity中创建一个空对象,并将三个目标点(可以是空对象或其他物体)分别赋值给 `target1`、`target2` 和 `target3` 变量。然后将此脚本挂载到需要执行三点随机移动的物体上。该脚本将使物体在给定的三个目标点之间进行随机移动,并且以 `speed` 变量定义的速进行移动。当物体到达一个目标点后,将会随机选择下一个目标点进行移动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值