c# - 从围绕 Z 的旋转获取方向

标签 c# algorithm unity3d math

我有飞机。它在 Y 轴上旋转 180 度,他的位置是 0,0,0 他面向 X 轴。这意味着我围绕 Z(欧拉角)旋转它以改变它面对的方向。我有云云有 ActionScript 。我希望云向平面在 Y 轴和 X 轴所面对方向的相反方向移动。

例如,当飞机的 rotation.eulerAngles.Z = 0 时,云应该全速向负 X 方向移动。而当 rotation.eulerAngles.Z = 90 时,云应该全速向负 Y 方向移动。旋转时。 eulerAngles.Z = 45 云应该以一半的速度向负 X 移动,以一半的速度向负 Y 移动,依此类推。

这里有两个插图,让您更容易形象化:

illustration 1 illustration 2

这里再放一张现场照片,方便大家想象:

the scene

这个想法是创造飞机在移动的错觉,实际上在这个场景中移动飞机会产生很多我真的不想处理的问题。

当前的云移动脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveCloud : MonoBehaviour
{

    public float speed;
    void Update()
    {
        var plane = GameObject.Find("plane");
        var dir =(plane.transform.position - plane.transform.forward)*speed*Time.deltaTime ;
        transform.position = new Vector3(transform.position.x + dir.x , transform.position.y + dir.y , transform.position.z);
        if (transform.position.x < -140)Destroy(gameObject);
    }
}

那么你会如何处理这个问题?

团结一致。

编辑: 我认为 trigo 在这里使用线性函数可能会有所帮助 这是我的新代码:

   public float speed;
    void Update()
    {

        var plane = GameObject.Find("plane");//Get the airplane
        var slope = Mathf.Tan(360 - plane.transform.rotation.eulerAngles.z);//Degrees to a slope (how much y it does in one x)
        var y = 1*slope;//kinda the very definition of the slope...
        var x = 1/slope;//math
        transform.position += new Vector3(x,y,0)*Time.deltaTime*speed;//the 1 , 1 point of our linear function(our direction) * delta * speed

        if (transform.position.x < -140)Destroy(gameObject);
    }

目前整个场景的云层都在摇晃。有时他们确实最终走向了正确的方向,但它并没有闲着。 想在我的数学上得到第二个意见。

我是如何解决这个问题的: 我刚刚做了

 transform.position += GameObject.Find("plane").transform.right * Time.deltaTime * speed;

感谢 PrinceOfRavens 帮助发现了这一点

最佳答案

飞机的蓝轴(z)是否朝向前方?使用您已实现的代码?云向什么方向移动?

统一向前是红轴(z)。如果平原方向正确,您的代码应该可以工作。找到来自 Nose 的轴,并记下颜色。 如果它是 : 蓝色你想要 -transform.right, 绿色你想要-transform.up

关于c# - 从围绕 Z 的旋转获取方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53694912/

相关文章:

unity3d - 了解 Unity 项目的 Unity 版本的方法?

c# - 如何使用 C# 在 sql 表中生成自动生成和自动更正的列

c# - DataGridView 单元格绘画 : Highlight word in cell considering cell's content direction

linux - 如何为 Linux (Arch) 手动安装旧版本的 unity (2019)

c - 在二进制数中找到第一个 "1"

algorithm - 理解调度以最小化迟到问题

iOS 11,Unity3d随机启动崩溃

c# - 在 Windows 中创建一个临时目录?

c# - 分层对象和 AutoFixture

algorithm - METIS 串行图分区器