Unity - 游戏对象朝向鼠标

5
我遇到了一个问题。
我目前的基本设置是,有两个对象:我的相机和我的玩家对象。
玩家通过WASD上的Transform移动,并应该在鼠标移动时旋转。
相机是俯视的(略带“3ps”风格的角度),可以保持玩家对象在相机视角中心,并根据玩家的旋转进行旋转。
这是玩家移动脚本:
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    public int playerSpeed = 8;  //players movement speed

    void Update () {
        if (Input.GetKey ("w")) 
        {
            transform.Translate (Vector3.forward * Time.deltaTime * playerSpeed); //move forward
        }  
        if (Input.GetKey ("s")) 
        {
            transform.Translate (Vector3.back * Time.deltaTime * playerSpeed); //move backwards
        }  
        if (Input.GetKey ("a")) 
        {
            transform.Translate (Vector3.left * Time.deltaTime * playerSpeed); //move left
        }  
        if (Input.GetKey ("d")) 
        {
            transform.Translate (Vector3.right * Time.deltaTime * playerSpeed); //move right
        }
    }
}

这是玩家旋转脚本:
using UnityEngine;
using System.Collections;

public class mouseLook : MonoBehaviour {
    private Vector3 inputRotation;
    private Vector3 mousePlacement;
    private Vector3 screenCentre;

    void Update () {
        FindCrap();
        transform.rotation = Quaternion.LookRotation(inputRotation);
    }

    void FindCrap () {
        screenCentre = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
        mousePlacement = Input.mousePosition;
        mousePlacement.z = mousePlacement.y;
        mousePlacement.y = 0;
        inputRotation = mousePlacement - screenCentre;
    } 
}

我展示的所有内容的结果是旋转,但它没有真正与鼠标物理位置一致地旋转。当我用鼠标画圆时,它会完全旋转,但不会始终指向鼠标所在位置。我不确定为什么。
期望的结果是相机(玩家对象的子对象)跟随玩家的移动和旋转,而玩家则使用其移动脚本移动并旋转以指向鼠标所在位置。
有任何想法吗?提前感谢您。
编辑:如果有帮助的话,当前的旋转方式如下:
用鼠标在玩家周围画大圆会使旋转速度变慢,而在玩家周围画非常紧密的圆则会使旋转速度变快。

据我所知,Unity中没有屏幕到世界坐标的功能,你需要捕获鼠标位置并将其转换为屏幕坐标。希望这至少能指引你正确的方向。请查看此文档:http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html - loekTheDreamer
2个回答

5

我不确定我是否理解您想做什么。如果您想做类似于游戏“死亡国度”的事情,那么我建议使用以下内容:

MouseLook.cs

void Update()
{
    Vector3 mouse = Input.mousePosition;
    Vector3 mouseWorld = Camera.main.ScreenToWorldPoint(new Vector3(
                                                        mouse.x, 
                                                        mouse.y,
                                                        player.transform.position.y));
    Vector3 forward = mouseWorld - player.transform.position;
    player.transform.rotation = Quaternion.LookRotation(forward, Vector3.up);
}

如果您希望相机随着玩家移动和旋转,则只需将相机设置为玩家对象的子级即可。

谢谢你的建议。看起来你的代码更适合我想要的,但是代码出现了错误。我已经定义了玩家参数,但是我收到的错误信息是:类型 'UnityEngine.Transform' 没有包含 'y' 的定义,也没有扩展方法 'y' (11,102) (CS1061)方法 'ScreenToWorldPoint' 没有接受 '3' 个参数的重载 (11,50) (CS1501) - Jordy Downie
所以,为了补充一下,它对这行代码提出了异议:Vector3 mouseWorld = Camera.main.ScreenToWorldPoint(mouse.x,mouse.y,player.transform.y); - Jordy Downie
你的回答确实解决了我的问题。但它导致了屏幕闪烁和鼠标移动反转。对此有什么想法吗? - Jordy Downie
倒置的鼠标移动(我假设问题仅发生在Y轴)应该通过将mouse.y替换为Screen.height - mouse.y来解决。关于闪烁,我不确定,但您是否将摄像机作为玩家的子对象? - Agustin0987
你肯定已经完成了80%的工作。我不明白关于Unity和C#的一件事,为什么不能通过坐标来定义移动和旋转?比如说:“如果(输入),{transform.translate(x,y+10,z)}”。这会让我的生活变得更加轻松。 - Jordy Downie
显示剩余12条评论

0

这是我用过的一个解决方案,但它只适用于2D。

同时,不要将相机作为玩家的子对象,而是使用像视频中展示的 slurping 脚本。

https://www.youtube.com/watch?v=MFQhpwc6cKE&t=3s&ab_channel=Brackeys

public Camera cam;
Transform my;


void Awake()
{
    cam = Camera.main;
    my = GetComponent<Transform>();
}


void Update()
{
    // Distance from camera to object.  We need this to get the proper calculation.
    float camDis = cam.transform.position.y - my.position.y;

    // Get the mouse position in world space. Using camDis for the Z axis.
    Vector3 mouse = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camDis));

    float AngleRad = Mathf.Atan2(mouse.y - my.position.y, mouse.x - my.position.x);
    float angle = (180 / Mathf.PI) * AngleRad;

    my.rotation = Quaternion.Euler(0, 0, angle);
}

您的答案可以通过提供更多支持信息来改进。请编辑以添加进一步细节,例如引用或文档,以便其他人可以确认您的答案是否正确。您可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接