本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!