Unity镜头跟随鼠标位置转换视野

原创 2018年04月16日 16:34:44
public class MouseLook : MonoBehaviour 
{ 
Vector2 _mouseAbsolute; 
Vector2 _smoothMouse;

public Vector2 clampInDegrees = new Vector2(360, 180);
public bool lockCursor;
public Vector2 sensitivity = new Vector2(2, 2);
public Vector2 smoothing = new Vector2(3, 3);
public Vector2 targetDirection;
public Vector2 targetCharacterDirection;

// Assign this if there's a parent object controlling motion, such as a Character Controller.
// Yaw rotation will affect this object instead of the camera if set.
public GameObject characterBody;

void Start()
{
    // Set target direction to the camera's initial orientation.
    targetDirection = transform.localRotation.eulerAngles;

    // Set target direction for the character body to its inital state.
    if (characterBody) targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
}

void Update()
{
    // Ensure the cursor is always locked when set
    //Screen.lockCursor = lockCursor;

    // Allow the script to clamp based on a desired target value.
    var targetOrientation = Quaternion.Euler(targetDirection);
    var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);

    // Get raw mouse input for a cleaner reading on more sensitive mice.
    var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

    // Scale input against the sensitivity setting and multiply that against the smoothing value.
    mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));

    // Interpolate mouse movement over time to apply smoothing delta.
    _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
    _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);

    // Find the absolute mouse movement value from point zero.
    _mouseAbsolute += _smoothMouse;

    // Clamp and apply the local x value first, so as not to be affected by world transforms.
    if (clampInDegrees.x < 360)
        _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);

    // Then clamp and apply the global y value.
    if (clampInDegrees.y < 360)
        _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);

    var xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
    transform.localRotation = xRotation;



    transform.localRotation *= targetOrientation;

    // If there's a character body that acts as a parent to the camera
    if (characterBody)
    {
        var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, characterBody.transform.up);
        characterBody.transform.localRotation = yRotation;
        characterBody.transform.localRotation *= targetCharacterOrientation;
    }
    else
    {
        var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
        transform.localRotation *= yRotation;
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35936174/article/details/79962539

同步调用和异步调用

前言:本文没什么创见,只是把有关内容做一个概括,希望能对新手有些用,也欢迎大家补充更正。      操作系统发展到今天已经十分精巧,线程就是其中一个杰作。操作系统把 CPU 处理时间划分成许多短暂时间...
  • xiaoran
  • xiaoran
  • 2002-05-16 17:19:00
  • 7994

Unity3d_镜头跟随鼠标移动

需要将摄像头放在另个GameObject里 当上下旋转的时候,只旋转摄像头(相当于人的头部) 当左右旋转的时候,旋转整体(相当于全身,包括头部) 这样使得整个视角都处于正对面,而不会出现倾斜等现...
  • amy260231120
  • amy260231120
  • 2017-12-11 18:38:35
  • 1457

unity3D鼠标滚轮来实现镜头拉近拉远及视角旋转

镜头拉近拉远的代码 在Unity开发中,用鼠标滚轮来实现摄像机移动控制视角变化或其它物体移动,代码如下: if( Input.GetAxis("Mouse ScrollWheel") != ...
  • alayeshi
  • alayeshi
  • 2015-03-26 10:57:23
  • 4677

Unity鼠标出屏幕后 摄像机跟随鼠标方向移动

using UnityEngine; using System.Collections;public class CameraHyp : MonoBehaviour { // Use this ...
  • tianduyingcai0113
  • tianduyingcai0113
  • 2017-09-09 11:33:49
  • 241

unity3d 摄像机跟随鼠标和键盘的控制

鼠标控制: using UnityEngine; using System.Collections; public class shubiao : MonoBehaviour { //...
  • hanghangaidoudou
  • hanghangaidoudou
  • 2015-02-01 07:00:20
  • 6284

【Unity3D】键盘鼠标控制视角的移动

在3D游戏里面,视角是一个很重要的因素,部分玩家对锁视角嗤之以鼻,因此视角也是游戏制作里面需要努力的方面。其实也不难,换到Unity3D里面是想办法控制摄像机的移动。下面举一个简单的例子说明在Unit...
  • yongh701
  • yongh701
  • 2017-05-02 16:38:03
  • 7963

Unity 3d 实现物体跟随摄像机视野运动

Unity 3d 实现物体跟随摄像机视野运动 Created by miccall (转载请注明出处 miccall.tech )1. VR实现机器人导航 - 项目里要求一个机器人跟随在...
  • qq_31411825
  • qq_31411825
  • 2017-03-12 18:12:13
  • 1024

unity3d--镜头跟随及控制(RPG游戏黑暗之光)

现需要完成镜头的跟随及控制。 一、镜头的跟随 思路很简单,首先取得镜头初始与角色的距离向量值,然后让相机与角色持续保持该向量距离即可。脚本如下: private Transform player; p...
  • Acmer_Sly
  • Acmer_Sly
  • 2016-10-16 18:29:49
  • 1900

简单的镜头跟随

void HelloWorld::setCenterOfScreen(CCPoint p) { CCSize s = CCDirector::sharedDirector()->getWinSize(...
  • z104207
  • z104207
  • 2015-07-17 13:53:36
  • 623

2d摄像机跟随脚本

using UnityEngine; using System.Collections; public class CameraFollow : MonoBehaviour  { public f...
  • haifeng619
  • haifeng619
  • 2014-12-31 13:22:23
  • 522
收藏助手
不良信息举报
您举报文章:Unity镜头跟随鼠标位置转换视野
举报原因:
原因补充:

(最多只允许输入30个字)