【问题标题】:Unity: Rotate towards mouse in 3D topdown viewUnity:在 3D 自上而下视图中向鼠标旋转
【发布时间】:2017-09-10 12:27:21
【问题描述】:

所以我试图创造一种像坦克一样的炮台。上半部分需要看我的鼠标。我试过了

GetComponent().ScreenToWorldPoint(Argument);

很多时候在一起

Input.mousePosition();

但我真的想不通。如果有人可以提供帮助或提供一个可以在 3D 环境中工作的简单脚本,我将在 C# 中对此进行编程,这将是很棒的!

到目前为止我的代码:

using UnityEngine;
using System.Collections;

public class Laser : MonoBehaviour {
    public float Speed;
    public AudioClip LaserSFX;
    private Transform Player;
    public Vector3 mousePos;
    public Camera Cam;

    // Use this for initialization
    void Start () {
        GameObject player = GameObject.FindWithTag("Player");
        Player = player.transform;
        if(!GetComponent<AudioSource>().isPlaying)
        GetComponent<AudioSource> ().PlayOneShot (LaserSFX);
    }

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

        //Code to look at mouse or rotate
        //Code to move towards it

        float dist = Vector3.Distance(Player.position, transform.position);
        if (dist >= 500) 
        {
            Destroy(gameObject);
        }
    }

}

致以诚挚的问候,

Misterk99

【问题讨论】:

  • 你能澄清一下这个问题吗?您的疑问是如何获得鼠标的坐标或旋转算法?
  • 我要做的就是拿到鼠标的绳子,然后将对象朝它旋转。由于我的相机旋转到自上而下的视图,mouse.y 轴是世界空间中的 z 轴。

标签: c# unity3d rotation mouse


【解决方案1】:

您必须将鼠标位置转换为视图位置,这意味着 ScreenToWorldPoint 应该由您当时使用的相机提供给您,并且由于您已经将相机作为 cam:

         Vector3 pos = Input.mousePosition;
         pos = Cam.ScreenToWorldPoint(pos);

现在您必须面对该位置,并且由于您使用的是自上而下的 3D,因此我假设相机面对 -y 位置。如果是这样,别忘了设置你想要的 y:

pos.y = (your value here, depending to the position you are giving to your objects);
Vectgor3 dir = this.position - pos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);

【讨论】:

  • 好吧,imgur.com/BSP90gg 宇宙飞船在 XYZ 0,0,0 处的视图是怎样的,所以鼠标 x 应该转换到世界 z 轴吧?
  • 当你使用 Cam.ScreenToWorldPoint(pos) 时,你已经完成了转换,你得到了相机看到鼠标的坐标
  • 现在我有了这个:Vector3 pos = Input.mousePosition; pos = Cam.ScreenToWorldPoint(pos); pos.y = 0; Vector3 dir = transform.position - pos; float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.AngleAxis(angle, Vector3.up); 而且由于某种原因它什么也没做
  • 尝试用 float angle = Mathf.Atan2(dir.z, dir.x);我忘了用 z 替换 y,因为我们处理的是 xz 平面而不是 xy。对不起xD
  • 这可要了我的命 xD 当我玩完之后它会旋转到某个点:Click for code 为什么这么难做?
【解决方案2】:

我在尝试为自己找到一个解决方案时想出了一个解决方案。 @Spike 给出的答案让我走上了正确的道路。但是,我使用了鼠标的视点位置并将对象位置转换为视口位置。这是 Javascript(我正在使用的)中的代码,应该很容易转换为 C#。

我的相机朝向 -Y 方向,仅供参考。

#pragma strict

var cam: Camera;

function Start () {

}

function Update () {

    var mousePos: Vector3 = Input.mousePosition;
    var playerPos: Vector3 = cam.WorldToScreenPoint(transform.position);

    var dir: Vector3 = mousePos - playerPos;

    var angle: float = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;

    transform.rotation = Quaternion.AngleAxis(-angle, Vector3.up);
}

【讨论】:

  • 好的...所以我知道这是一岁了,但是如果我的相机没有面向 -Y 方向怎么办?我不知道你的代码是如何确定的。
  • 没关系。我想到了。我只是从角度减去90。我还将 Vector3 反转为 Vector3.down。如果您不这样做,只需将角度添加 90。我的相机朝向 +Z 轴:transform.rotation = Quaternion.AngleAxis(angle + -90, Vector3.down);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多