【问题标题】:Mouse dragging detection - Unity3D鼠标拖动检测 - Unity3D
【发布时间】:2015-06-17 09:57:26
【问题描述】:

我正在制作一款游戏,让玩家在聚光灯下探索几个地牢(以第一人称视角)。我已经让玩家移动了,但我实际上对相机旋转有疑问。我一直在寻找一些论坛,但我的问题并不完全相同,而且我是 Unity 的新手。问题是:如何在屏幕上拖动鼠标的同时移动我的相机,让玩家看到他周围发生的事情?

【问题讨论】:

标签: unity3d camera mouse drag


【解决方案1】:
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 );
    }
}

【讨论】:

    【解决方案2】:

    检查这个http://unity3d.com/learn/tutorials/projects/survival-shooter/player-character

    void OnUpdate() { Turning() }

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

    【讨论】:

      猜你喜欢
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 2022-07-25
      • 2020-05-31
      • 2020-10-09
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      相关资源
      最近更新 更多