- Home /
How can I pause the first person character camera rotation in this case??
I am working on a method of inspecting objects. When I select the object, it approaches the camera and I can examine it by rotating, as shown in the image below:
I want to pause the first person character camera rotation while I'm examining the object. I created a script where I try to get the camera sensors from another script called "MouseLook" and set it to 0. But when I select the object in the game, the error appears:
"ArgumentException: GetComponent requires that the requested component 'MouseLook' derives from MonoBehaviour or Component or is an interface. UnityEngine.Component.GetComponent[T] () (at :0) UnityStandardAssets.Characters.FirstPerson.ExaminarObjetos.ClickObject () (at Assets/Scripts/ExaminarObjetos.cs:61) UnityStandardAssets.Characters.FirstPerson.ExaminarObjetos.Update () (at Assets/Scripts/ExaminarObjetos.cs:32)"
The script I created to examine the object is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityStandardAssets.Characters.FirstPerson
{
public class ExaminarObjetos : MonoBehaviour
{
Camera mainCam;//Camera Object Will Be Placed In Front Of
GameObject clickedObject;//Currently Clicked Object
public float SensorX = 0;
public float SensorY = 0;
//Holds Original Postion And Rotation So The Object Can Be Replaced Correctly
Vector3 originaPosition;
Vector3 originalRotation;
//If True Allow Rotation Of Object
bool examineMode;
void Start()
{
mainCam = Camera.main;
examineMode = false;
}
private void Update()
{
ClickObject();//Decide What Object To Examine
TurnObject();//Allows Object To Be Rotated
ExitExamineMode();//Returns Object To Original Postion
}
void ClickObject()
{
if (Input.GetMouseButtonDown(0) && examineMode == false)
{
RaycastHit hit;
Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
//ClickedObject Will Be The Object Hit By The Raycast
clickedObject = hit.transform.gameObject;
//Save The Original Postion And Rotation
originaPosition = clickedObject.transform.position;
originalRotation = clickedObject.transform.rotation.eulerAngles;
//Now Move Object In Front Of Camera
clickedObject.transform.position = mainCam.transform.position + (transform.forward * 1.5f);
//Pause The Game
Time.timeScale = 0;
SensorX = GetComponent<MouseLook>().XSensitivity;
SensorY = GetComponent<MouseLook>().YSensitivity;
//Turn Examine Mode To True
examineMode = true;
}
}
}
void TurnObject()
{
if (Input.GetMouseButton(2) && examineMode)
{
float rotationSpeed = 15;
float xAxis = Input.GetAxis("Mouse X") * rotationSpeed;
float yAxis = Input.GetAxis("Mouse Y") * rotationSpeed;
clickedObject.transform.Rotate(Vector3.up, -xAxis, Space.World);
clickedObject.transform.Rotate(Vector3.right, yAxis, Space.World);
}
}
void ExitExamineMode()
{
if (Input.GetMouseButtonDown(1) && examineMode)
{
//Reset Object To Original Position
clickedObject.transform.position = originaPosition;
clickedObject.transform.eulerAngles = originalRotation;
//Unpause Game
Time.timeScale = 1;
//Return To Normal State
examineMode = false;
}
}
}
}
And the MouseLook script that I'm trying to get the components is:
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
namespace UnityStandardAssets.Characters.FirstPerson
{
[Serializable]
public class MouseLook
{
public float XSensitivity = 2f;
public float YSensitivity = 2f;
public bool clampVerticalRotation = true;
public float MinimumX = -90F;
public float MaximumX = 90F;
public bool smooth;
public float smoothTime = 5f;
public bool lockCursor = true;
private Quaternion m_CharacterTargetRot;
private Quaternion m_CameraTargetRot;
private bool m_cursorIsLocked = true;
public void Init(Transform character, Transform camera)
{
m_CharacterTargetRot = character.localRotation;
m_CameraTargetRot = camera.localRotation;
}
public void LookRotation(Transform character, Transform camera)
{
float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;
m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
if(clampVerticalRotation)
m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
if(smooth)
{
character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
smoothTime * Time.deltaTime);
camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
smoothTime * Time.deltaTime);
}
else
{
character.localRotation = m_CharacterTargetRot;
camera.localRotation = m_CameraTargetRot;
}
UpdateCursorLock();
}
public void SetCursorLock(bool value)
{
lockCursor = value;
if(!lockCursor)
{//we force unlock the cursor if the user disable the cursor locking helper
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
public void UpdateCursorLock()
{
//if the user set "lockCursor" we check & properly lock the cursos
if (lockCursor)
InternalLockUpdate();
}
private void InternalLockUpdate()
{
if(Input.GetKeyUp(KeyCode.Escape))
{
m_cursorIsLocked = false;
}
else if(Input.GetMouseButtonUp(0))
{
m_cursorIsLocked = true;
}
if (m_cursorIsLocked)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else if (!m_cursorIsLocked)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
Quaternion ClampRotationAroundXAxis(Quaternion q)
{
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1.0f;
float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
return q;
}
}
}
How can i use GetComponent to pause the first person character camera rotation in this case??
Answer by hexagonius · Apr 27, 2020 at 07:46 PM
The error couldn't be more clear:
ArgumentException: GetComponent requires that the requested component 'MouseLook' derives from MonoBehaviour or Component or is an interface
MouseLook is not attached to any gameobject and cannot be because it does not derive from MonoBehaviour, which it should if you want to use it as a component. If you don't, just turn your Init into a regular constructor and create an instance of the class in whatever class needs to reference it and can inject the relevant parts.
Your answer
Follow this Question
Related Questions
How to pan camera based on mouse position 0 Answers
get script of another gameobject in c# 1 Answer
I want to create an interactive right analog MouseLook? 0 Answers
Multiple Cars not working 1 Answer