简体   繁体   English

如何在 Unity 中检测鼠标是否在屏幕上移动?

[英]How do I Detect if the Mouse is Moving on Screen in Unity?

I am making a 360 camera movement for my game called PROTOTYPE.我正在为我的名为 PROTOTYPE 的游戏制作 360 度摄像机移动。 I need a function or something to detect if the mouse is moving and in which direction on the x axis it is, to set an offset for the Camera Smoothing script, but I don't know any.我需要一个函数或其他东西来检测鼠标是否在移动以及它在 x 轴上的哪个方向,以设置相机平滑脚本的偏移量,但我不知道。 Could somebody plz help me?有人可以帮助我吗? Here is the Camera Smoothing script if u need it:如果需要,这是相机平滑脚本:

using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Transform target;
    
    public float smoothSpeed = 0.125f;
    
    public Vector3 offset;
    
    void FixedUpdate()
    {
        Vector3 desiredPosition = target.position + offset;
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
        transform.position = smoothedPosition;
    
        transform.LookAt(target);
    }
}

I suppose you want to move your camera with your mouse, 360° around your character and also that script is attached to your player.我想你想用你的鼠标移动你的相机,围绕你的角色 360° 并且该脚本也附加到你的播放器上。

In order to do that, your FixedUpdate should look like this:为此,您的 FixedUpdate 应如下所示:

void FixedUpdate()
{
    float horizontalAxis = Input.GetAxis("Mouse X"); // Getting the current mouse axis (left-right)
    float verticalAxis = Input.GetAxis("Mouse Y"); // Getting the current mouse axis (up-down)

    transform.RotateAround(player.transform.position, -Vector3.up, horizontalAxis * smoothSpeed);
    transform.RotateAround(Vector3.zero, transform.right, verticalAxis * smoothSpeed);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM