鼠标拖动检测- Unity3D

8ehkhllq  于 2022-12-23  发布在  其他
关注(0)|答案(2)|浏览(140)

我正在做一个游戏,玩家有一个聚光灯,并探索了一些地牢(第一人称)。我已经让玩家移动,但我实际上有一个相机旋转的疑问。我一直在寻找一些论坛,但问题不完全相同,在我的,我是新的团结。问题是:我怎样才能移动我的摄像机,同时在屏幕上拖动鼠标,让玩家看到他周围发生了什么?

ma8fv8wu

ma8fv8wu1#

选中此项http://unity3d.com/learn/tutorials/projects/survival-shooter/player-character
更新时无效(){旋转()}

void Turning ()
{
    // Create a ray from the mouse cursor on screen in the direction of the camera.
    Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);

    // Create a RaycastHit variable to store information about what was hit by the ray.
    RaycastHit floorHit;

    // Perform the raycast and if it hits something on the floor layer...
    if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    {
        // Create a vector from the player to the point on the floor the raycast from the mouse hit.
        Vector3 playerToMouse = floorHit.point - transform.position;

        // Ensure the vector is entirely along the floor plane.
        playerToMouse.y = 0f;

        // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
        Quaternion newRotation = Quaternion.LookRotation (playerToMouse);

        // Set the player's rotation to this new rotation.
        playerRigidbody.MoveRotation (newRotation);
    }
}
ctzwtxfj

ctzwtxfj2#

using UnityEngine;
using System.Collections;

public class MouseView : MonoBehaviour 
{
    public float Sensitivity = 0.125f;

    Vector3 _prevMousePosition = Vector3.zero;
    float   _tilt = 0;
    float   _turn = 0;

    void Update () 
    {       
        Vector3 mousePosition = Input.mousePosition;
        if( Input.GetMouseButton( 0 ) )
        {
            _tilt += -( mousePosition.y-_prevMousePosition.y ) * Sensitivity;
            _turn += ( mousePosition.x-_prevMousePosition.x ) * Sensitivity;
        }
        _prevMousePosition = mousePosition;
        transform.localRotation = Quaternion.Euler( _tilt, _turn, 0 );
    }
}

相关问题