如何倾斜某个物体?

时间:2019-02-21 17:43:22

标签: c# unity3d

public class moveball : MonoBehaviour
{
    public GameObject player;

    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.Find("Whyareyoulikethis");
        transform.Translate(player.transform.rotation.x, player.transform.rotation.y, 0);
    }

    // Update is called once per frame
    void Update()
    {
    }
}

我写了这段代码。它放置在一个对象(称为baller)的内部,当按下空格键时,该对象将由另一个(玩家)实例化。它应该以与玩家面对的角度相同的角度移动(X是玩家更改的唯一旋转值,Y始终为90,Z始终为0)。

尽管我的逻辑令人难以置信,但我在这里所做的只是我最成功的尝试,目的是使它按预期工作。这行不通,但是我不知道会怎样。当transform.Translate放置在Update()中时,它将以偏移量拍摄自己,但以其他方式接近播放器的角度。在开始时,它不会移动,因为我没有给它一个速度。

1 个答案:

答案 0 :(得分:1)

尽管不清楚您对move an object at an angle的确切含义,Transform.rotation的类型为Quaternion,具有4个值x,y,z,w。所以在

中使用它们
transform.Translate(player.transform.rotation.x, player.transform.rotation.y, 0);

将永远不会返回您期望的值。

您想要的可能是Transform.eulerAngles,它会给您Vector3


但是你说It is supposed to move at the same angle that player is facing还是应该像

transform.Translate(player.transform.forward);

使用forward矢量指向玩家面对的方向