unity-Gameobject看鼠标 [英] Unity - Gameobject look at mouse

查看:152
本文介绍了unity-Gameobject看鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题.

我现在拥有的基本设置是两个对象:我的相机和我的播放器对象.

The basic setup I have right now, two objects: my camera, and my player object.

播放器通过WASD上的Transform进行移动,并且应该在鼠标上旋转 移动.

The player moves via Transform on WASD, and is supposed to rotate on mouse movement.

摄像头是自上而下的(以微小的"3ps"样式角度,使播放器对象的中心保持在摄像头的角度,并根据播放器的旋转而旋转.

The camera is top down (At a slight "3ps" style angle, which keeps the player object centre to the camera's perspective, and rotates according to the players rotation.

这是玩家移动脚本:

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;
    } 
}

我所展示的一切结果都是旋转,但并不能真实地旋转到鼠标的实际位置.

The result of everything I have shown is it rotates, but it doesn't rotate true to where the mouse physically is.

当我用鼠标画圆时,它将进行完整的旋转,但并不能始终指向鼠标所在的位置.我不确定为什么.

While I draw circles with the mouse, it will do full rotations, but not consistently point to where the mouse is. I'm not sure as to why.

理想的结果是使摄像机(玩家对象的子代)跟随玩家的移动和旋转,同时玩家随其移动脚本移动,并旋转以将鼠标指向真实位置.

The desired result is for the camera (child of player object) to follow the players movement and rotation, while the player moves with its movement script, and rotates to point true to where the mouse is.

有人有什么想法吗?预先感谢.

Anyone got any ideas? Thanks in advance.

如果有帮助,当前的旋转方式如下.

if it helps, the current rotation works like this.

用鼠标在播放器周围绘制大圆圈比旋转在播放器周围极紧的圆圈要慢.

drawing large circles with the mouse around the player gives a slower rotation, than extremely tight circles around the player.

推荐答案

我不确定是否了解您要执行的操作.如果您尝试做类似于"Dead Nation"游戏的事情,那么我会建议这样的事情:

I'm not sure If I understand what you are trying to do. If you are trying to do something similar to the game "Dead Nation", then I would suggest something like this:

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);
}

如果您希望摄像机随播放器一起移动和旋转,则只需将摄像机作为播放器对象的子项即可.

If you want the camera to move and rotate along with the player then just make the camera a child of the player object.

这篇关于unity-Gameobject看鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆