我刚开始学习编程并且对这个论坛也不太熟悉,如果我违反了任何规定,请见谅。我只是在尝试制作一个第三人称摄像机而已,但是我一直遇到以下问题:
未分配引用异常:CameraFollow的CameraFollowObj变量尚未分配。 您可能需要在检查器中为CameraFollow脚本分配CameraFollowObj变量。 CameraFollow.CameraUpdater() (位于Assets/Scripts/CameraFollow.cs:68) CameraFollow.LateUpdate() (位于Assets/Scripts/CameraFollow.cs:62)
我创建了一个对象让摄像机跟随并将其放在模型上。然后将该对象移动到我认为是正确的字段,但问题仍然存在。
未分配引用异常:CameraFollow的CameraFollowObj变量尚未分配。 您可能需要在检查器中为CameraFollow脚本分配CameraFollowObj变量。 CameraFollow.CameraUpdater() (位于Assets/Scripts/CameraFollow.cs:68) CameraFollow.LateUpdate() (位于Assets/Scripts/CameraFollow.cs:62)
我创建了一个对象让摄像机跟随并将其放在模型上。然后将该对象移动到我认为是正确的字段,但问题仍然存在。
![](https://cdn.discordapp.com/attachments/579458237938532352/616496662059286537/unknown.png)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public float CameraMoveSpeed = 120.0f;
public GameObject CameraFollowObj;
Vector3 FollowPOS;
public float clampAngle = 80.0f;
public float InputSensitivity = 150.0f;
public GameObject CameraObj;
public GameObject PlayerObj;
public float camDistanecXToPlayer;
public float camDistanecYToPlayer;
public float camDistanecZToPlayer;
public float mouseX;
public float mouseY;
public float finalInputX;
public float finalInputZ;
public float smoothX;
public float smoothY;
private float rotY = 0.0f;
private float rotX = 0.0f;
// Start is called before the first frame update
void Start()
{
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
float InputX = Input.GetAxis("RightStickHorizontal");
float InputZ = Input.GetAxis("RightStickVertical");
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
finalInputX = InputX + mouseX;
finalInputZ = InputZ + mouseY;
rotY += finalInputX * InputSensitivity * Time.deltaTime;
rotX += finalInputZ * InputSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
transform.rotation = localRotation;
}
void LateUpdate()
{
CameraUpdater();
}
void CameraUpdater()
{
Transform target = CameraFollowObj.transform;
float step = CameraMoveSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards (transform.position, target.position, step);
}
}