我有一架飞机 . 它在Y上旋转180度,他的位置是0,0,0,他面向X轴 . 这意味着我围绕Z(欧拉角)旋转它以改变它所面对的方向 . 我有 Cloud , Cloud 有一个运动脚本 . 我希望 Cloud 在与Y轴和X轴面对的方向相反的方向上移动 .
例如,当飞机的rotation.eulerAngles.Z = 0时, Cloud 应该全速移向负X.当rotation.eulerAngles.Z = 90时, Cloud 应该全速移向负Y.当rotation.eulerAngles.Z = 45 Cloud 应该以半速向负X移动,半速向负Y移动,依此类推 .
这里有两个插图,让您更容易想象:
这里是一张场景照片,让您更容易想象:
这个想法是为了创造飞机正在移动的错觉,实际上在这个场景中移动飞机会产生很多我真正不想处理的问题 .
当前的 Cloud 移动脚本:
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);
}
目前, Cloud 层正在震动整个场景 . 有时候他们最终会走向正确的方向,但并不是空闲的 . 想在我的数学上得到第二个意见 .
我是如何解决这个问题的:我刚刚做到了
transform.position += GameObject.Find("plane").transform.right * Time.deltaTime * speed;
感谢PrinceOfRavens帮助我找到了这个