I’m new to Unity. Super simple question. All the other answers I can find are either from 2010 with the wrong syntax or give me an answer like use FindGameObject().
Whatever I end up doing with the newer answers gives me a red squiggly underneath the method.
My code doesn’t recognize other objects, and doesn’t recognize variables within them.
What I’m trying to do:
Have an object (DialogueInteraction.cs), interact with the FPSController and switch a variable declared in MouseLook (public bool enabled) from true to false and vice versa. MouseLook is called within FirstPersonController() but isn’t called directly itself. I’m not sure if that plays into it. The objects are set to defaults with the exception of adding the “enabled” variable into MouseLook.
I had to do a quick search to see that script, as I don’t have it easily available atm.
That class is not a component, from what I saw, and explains why that code doesn’t work.
You can try to get the reference to mouse look through the FPSController instead?
It should just be a simple line of code, right? I can’t find the answer. I’ve seen a bunch of questions like this but I always get a red squiggly under whatever the solution is.
That script isn’t a component. And what you are accessing isn’t a variable in the script, it’s the enabled part to turn on or off a component on a gameobject, which does nothing since the MouseLook can’t be attached to a gameobject.
I was wondering if that’s the kind of enabled he was looking for… But thought maybe it was in the script
Perhaps some other means of reaching your goal is in order.
What I’m trying to accomplish is change the variable “enabled”, which I declared in MouseLook, to true. When enabled is set to false, it exists most of the methods without executing them. This is so the player can use the interface without the cursor being locked to the center of the screen and moving the view around.
This is what my MouseLook script looks like, it is the same as the default except with the enabled variable added:
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;
//public bool enabled { get; set; }
public bool enabled = 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;
enabled = false;
}
public void LookRotation(Transform character, Transform camera)
{
if(enabled == false)
{
return;
}
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)
{
if(enabled == false)
{
return;
}
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(enabled == false)
{
return;
}
//if the user set "lockCursor" we check & properly lock the cursos
if (lockCursor)
InternalLockUpdate();
}
private void InternalLockUpdate()
{
if(enabled == false)
{
return;
}
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;
}
}
}
I figured it out. Brathnann, thanks for telling me that MouseLook was a variable on its own. Knowing that allowed me to search for different answers and I figured it out eventually.