【发布时间】:2020-10-10 15:53:19
【问题描述】:
我在第三人称 3D 游戏中有一辆坦克,所以我的相机从上方和侧面看坦克。我需要坦克的炮塔在 y 轴上旋转,以便它始终注视鼠标并在玩家点击的地方射击。问题是我的游戏是 3D 的,而我得到的只是它实际上看着鼠标(就像向上看天空一样)。
这是我现在的代码:
private Vector3 target;
private Camera cam;
private void Start()
{
cam = Camera.main;
}
void Update()
{
Vector2 mousePos = new Vector2();
mousePos.x = Input.mousePosition.x;
mousePos.y = Input.mousePosition.y;
target = cam.WorldToScreenPoint(new Vector3(mousePos.x, mousePos.y, cam.nearClipPlane));
transform.LookAt(target);
}
【问题讨论】:
-
请确保使用正确的标签。您的代码位于
c#...unityscript是/曾经是一种 JavaScript 风格,类似于 Unity 以前使用的自定义语言,现在早已弃用
标签: c# unity3d game-development