unity鼠标控制镜头旋转_求教,人物控制,视角随鼠标移动,且绕角色旋转。
发布日期:2021-10-30 18:55:29 浏览次数:4 分类:技术文章

本文共 2336 字,大约阅读时间需要 7 分钟。

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

小弟是美工,刚接触编脚本没几天,今天试着修改了一下unity3d 自带的视角控制脚本MouseLook.cs

其中第45行是小弟添加的,实现了绕角色旋转的功能,但是碰到一个问题,就是视角的角度和绕角色旋转的移动速度不一致。而数学对于我来说实在是无力。

求教在这个脚本中视角角度和旋转移动速度的关系。

附上脚本

using UnityEngine;

using System.Collections;

/// MouseLook rotates the transform based on the mouse delta.

/// Minimum and Maximum values can be used to constrain the possible rotation

/// To make an FPS style character:

/// - Create a capsule.

/// - Add the MouseLook script to the capsule.

/// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)

/// - Add FPSInputController script to the capsule

/// -> A CharacterMotor and a CharacterController component will be automatically added.

/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.

/// - Add a MouseLook script to the camera.

/// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)

[AddComponentMenu("Camera-Control/Mouse Look")]

public class MouseLook : MonoBehaviour {

public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }

public RotationAxes axes = RotationAxes.MouseXAndY;

public float sensitivityX = 30F;

public float sensitivityY = 30F;

public float minimumX = -360F;

public float maximumX = 360F;

public float minimumY = -60F;

public float maximumY = 60F;

public float rotationspeed;

public GameObject center;

float rotationY = 0F;

void Update ()

{

if (axes == RotationAxes.MouseXAndY)

{

float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);

transform.RotateAround(center.transform.position,Vector3.up,Input.GetAxis("Mouse X") * sensitivityX);//我在这里 ,我是第45行。

//我在这里 ,我是第45行。

//我在这里 ,我是第45行。

}

else if (axes == RotationAxes.MouseX)

{

transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);

}

else

{

rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);

}

}

void Start ()

{

// Make the rigid body not change rotation

if (GetComponent())

GetComponent().freezeRotation = true;

}

}

转载地址:https://blog.csdn.net/weixin_39849239/article/details/111539205 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:好消息acesse_【好消息】“Access Bars能量清理疗愈”公益讲座报名即日开始!
下一篇:hdfs是nas_hdfs 存储空间

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月11日 18时54分09秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章