- Home /
Question by
ksandresh1 · May 29 at 04:54 PM ·
character controllercharacter movement
I tried to apply the head rotation and this problem is seen
UnassignedReferenceException: The variable playerBody of MouseLook has not been assigned. You probably need to assign the playerBody variable of the MouseLook script in the inspector. UnityEngine.Transform.get_localRotation () (at :0) UnityEngine.Transform.Rotate (UnityEngine.Vector3 eulers, UnityEngine.Space relativeTo) (at :0) UnityEngine.Transform.Rotate (UnityEngine.Vector3 eulers) (at :0) MouseLook.Update () (at Assets/MouseLook.cs:26)
// Code starts from here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, -90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
Comment
You need to drag an object to playerBody
in the inspector, it is not assigned.
Answer by Hawaii_Dev · May 31 at 10:16 AM
As the error message says, you have probably forgotten to drag the transform of the playerBody into the public field in the inspector labeled PlayerBody.