从绕Z轴的旋转中获得方向



我有一个平面。它在Y轴上旋转180度,其位置为0,0,0,面向X轴。这意味着我绕着Z(欧拉角(旋转它,以改变它所面对的方向。我有云,云有运动脚本。我希望云的移动方向与平面在Y轴和X轴上所面对的方向相反。

例如,当平面的旋转.eulerAngles.Z=0时。云应该以全速向负X移动。当旋转时。eulerAngles.Z=90云应该以低速向负Y移动。当自转时。euler Angles.Z=45云应该以半速向负X和半速向Y移动,依此类推。

这里有两个插图可以让你更容易地看到:

插图1图2

这是一张场景的照片,让你更容易看到:

场景

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

当前云移动脚本:

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(。如果平原方向正确,代码应该可以工作。找到来自鼻子的轴,并记下颜色。如果:蓝色你想要CCD_ 1,绿色,你想要-transform.up

最新更新