但是玩家应该在世界空间中用头脑环顾四周。例如,如果我移动鼠标光标并指向山峰,玩家应该看看山峰。但主要目标是在游戏运行时只需在游戏窗口中移动鼠标即可旋转头部。
也许我也不需要这么多的旋转,速度,x,y变量?也许我可以为所有这些使用更少的变量。我想保留脚本中的其他部分并添加头部。
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseOrbitImproved : MonoBehaviour
{
public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = 3.0f;//-20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
public Transform boneToRotate;
int speed;
float friction;
float lerpSpeed;
private float xDeg;
private float yDeg;
private Quaternion fromRotation;
private Quaternion toRotation;
private Rigidbody rigidbody;
float x = 0.0f;
float y = 0.0f;
public float rotationSpeed = 5f;
// Use this for initialization
void Start()
{
speed = 3;
friction = 3f;
lerpSpeed = 3f;
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
rigidbody = GetComponent<Rigidbody>();
// Make the rigid body not change rotation
if (rigidbody != null)
{
rigidbody.freezeRotation = true;
}
}
void LateUpdate()
{
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
{
xDeg -= Input.GetAxis("Mouse X") * speed * friction;
yDeg += Input.GetAxis("Mouse Y") * speed * friction;
fromRotation = boneToRotate.rotation;
toRotation = Quaternion.Euler(yDeg, xDeg, 0);
boneToRotate.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * lerpSpeed);
Debug.Log("Mouse Freedom");
}
if (target && Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotationSpeed);
if (target != null)
{
target.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotationSpeed);
}
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
脚本附加到主摄像头。 在boneToRotate变量中,我将EthanHead从ThirdPersonController中拖出。
我刚刚添加的用于旋转头部的部分是: 在顶部:
public Transform boneToRotate;
int speed;
float friction;
float lerpSpeed;
private float xDeg;
private float yDeg;
private Quaternion fromRotation;
private Quaternion toRotation;
开始时间:
speed = 3;
friction = 3f;
lerpSpeed = 3f;
更新:
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
{
xDeg -= Input.GetAxis("Mouse X") * speed * friction;
yDeg += Input.GetAxis("Mouse Y") * speed * friction;
fromRotation = boneToRotate.rotation;
toRotation = Quaternion.Euler(yDeg, xDeg, 0);
boneToRotate.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * lerpSpeed);
Debug.Log("Mouse Freedom");
}
但它根本不起作用。用鼠标光标头完全没有旋转。
答案 0 :(得分:0)
首先使用适当的值初始化变量 -
void Start()
{
speed = 3;
friction = 3f;
lerpSpeed = 3f;
xDeg = 0f;
yDeg = 0f;
fromRotation = boneToRotate.rotation;
toRotation = boneToRotate.rotation;
...
...
}
然后将lerp函数移出if条件 -
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
{
xDeg -= Input.GetAxis("Mouse X") * speed * friction;
yDeg += Input.GetAxis("Mouse Y") * speed * friction;
fromRotation = boneToRotate.rotation;
toRotation = Quaternion.Euler(yDeg, xDeg, 0);
Debug.Log("Mouse Freedom");
}
boneToRotate.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * lerpSpeed);