c# - Unity试图相机轨道控制器
问题描述
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camMouseLook : MonoBehaviour {
protected Transform XForm_camera;
protected Transform XForm_Parent;
protected Vector3 LocalRotation;
protected float CameraDistance = 10f;
public float MouseSensitivity = 4f;
public float ScrollSensitivity = 2f;
public float OrbitSpeed = 10f;
public float ScrollSpeed = 6f;
public bool CameraDisabled = false;
void Start () {
this.XForm_camera = this.transform;
this.XForm_Parent = this.transform.parent;
}
// Update is called once per frame
void LateUpdate () {
if (Input.GetKeyDown(KeyCode.LeftShift))
{
CameraDisabled = !CameraDisabled;
}
if (!CameraDisabled)
{
if(Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
{
LocalRotation.x += Input.GetAxis("Mouse X") * MouseSensitivity;
LocalRotation.y -= Input.GetAxis("Mouse Y") * MouseSensitivity;
LocalRotation.y = Mathf.Clamp(LocalRotation.y, 0f, 90f);
}
if(Input.GetAxis("Mouse ScrollWheel") != 0f)
{
float ScrollAmount = Input.GetAxis("Mouse ScrollWheel") *
ScrollSensitivity;
ScrollAmount *= (this.CameraDistance * 0.3f);
this.CameraDistance += ScrollAmount * -1f;
this.CameraDistance = Mathf.Clamp(this.CameraDistance, 1.5f, 100f);
}
}
Quaternion QT = Quaternion.Euler(LocalRotation.y, LocalRotation.x, 0);
this.XForm_Parent.rotation = Quaternion.Lerp(this.XForm_Parent.rotation, QT, Time.deltaTime * OrbitSpeed);
if (this.XForm_camera.localPosition.z != this.CameraDistance * -1f)
{
this.XForm_camera.localPosition = new Vector3(0f, 0f, Mathf.Lerp(this.XForm_camera.localPosition.z, this.CameraDistance * -1f, Time.deltaTime * ScrollSpeed));
}
}
}
我得到一个对象引用未设置为对象错误的实例。我在“this.XForm_Parent.rotation=Quaternion.Lerp(this.XForm_Parent.rotation, QT, Time.deltaTime * OrbitSpeed);”行收到此错误。希望有人可以帮助我在这方面苦苦挣扎了很长时间。
解决方案
您只需要将游戏对象附加到父对象,因为您告诉 this.XForm_Parent = this.transform.parent 并且游戏对象没有附加父对象;)
推荐阅读
- c++ - 你好,其实我是想看懂下面的代码。有人可以详细说明代码到底在做什么。为什么它在第 31 行崩溃
- java - 如何准确知道 NullPointerException 来自何处?
- python - 在 Python 上用 CSV 中的另一列替换列
- r - 获取R中给定因子水平的标签
- asp.net-core-mvc - 如何将视图组件数据与局部视图同步?
- excel - 是(1)/否(0)通过在excel中应用多个条件输出
- sql - SQL 获取当前月份
- python - Python QT findChildren 没有从 UI 文件中找到任何子项
- php - 尽管 JSON 有效,但 cURL 导致 NULL
- c++ - 类型数组?