Boolean variable is never true, even if declared true or ticked in the inspector
Hello, I am implementing a feature on the scene's camera in which it follows a gameobject but it still allows you to pan around, this is a toggleable behavior so it requires a boolean variable to tell the script when or when not to do this algorithm.
Here in lies the problem, no matter what, that boolean variable is ALWAYS false, even if it is declared true or if it is ticked on the inspector
as far as I can tell, there is nothing in the script that should change the value of isFocusing (I even went to as far as commenting out the function that changed it)
The only way I currently have to set isFocusing to true is to change it through the visual studio debug tool Here's the script, key part of it are "private GameObject focus;" and "private void updatePosition()"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class MONO_CameraController : MonoBehaviour
{
// input action asset stuff
private CameraInputAction cam_inputAction;
private InputAction cam_movment;
//camera position
private Transform cam_transform;
// following object
[SerializeField]
private GameObject focus;
private Vector3 focus_offset;
[SerializeField]
private bool isFocusing;
// movment stuff
[SerializeField]
private float maxSpeed = 5f;
private float speed;
[SerializeField]
private float acceleration = 10f;
[SerializeField]
private float dampening = 15f;
// zooming stuff
[SerializeField]
private float stepsize = 2f;
[SerializeField]
private float zoomDapening = 7.5f;
private float zoomHeight;
[SerializeField]
private float minHeight = 5f;
[SerializeField]
private float maxHeight = 50f;
[SerializeField]
private float zoomSpeed = 2f;
// general stuff
Vector3 lastPostion;
Vector3 movementVelocity;
Vector3 targetPosition;
private void Awake()
{
cam_inputAction = new CameraInputAction();
cam_transform = GetComponentInChildren<Camera>().transform;
}
private void OnEnable()
{
zoomHeight = cam_transform.localPosition.z;
lastPostion = this.transform.position;
cam_movment = cam_inputAction.Camera.Movment;
cam_inputAction.Camera.Enable();
cam_inputAction.Camera.Zoom.performed += getTargetZoomLevel;
}
private void OnDisable()
{
cam_inputAction.Camera.Disable();
cam_inputAction.Camera.Zoom.performed -= getTargetZoomLevel;
}
private void LateUpdate()
{
getKeyboardMovment();
updateVelocity();
updatePosition();
updateZoomLevel();
}
private void getTargetZoomLevel(InputAction.CallbackContext inputvalue)
{
float val = -inputvalue.ReadValue<Vector2>().y /20f;
if (Mathf.Abs(val) >0.1f)
{
zoomHeight = cam_transform.localPosition.z + val * stepsize;
if (zoomHeight > minHeight)
zoomHeight = minHeight;
else if(zoomHeight < maxHeight)
zoomHeight = maxHeight;
}
}
private void updateZoomLevel()
{
Vector3 zoomTarget = new Vector3(cam_transform.localPosition.x, cam_transform.localPosition.y, zoomHeight);
zoomTarget.z -= zoomSpeed * (zoomHeight - zoomTarget.z);
cam_transform.localPosition = Vector3.Lerp(cam_transform.localPosition, zoomTarget, Time.deltaTime * zoomDapening);
maxSpeed = -zoomTarget.z;
}
private void getFocusOffset()
{
focus_offset = focus.transform.position;
Debug.Log(focus.transform.position);
focus_offset.z = 0;
}
/*private bool toggleFocus()
{
isFocusing = !isFocusing;
Debug.Log("camera is focusing on " + focus);
return isFocusing;
}*/
private void updateVelocity()
{
movementVelocity = (this.transform.position - lastPostion)/Time.deltaTime;
movementVelocity.y = 0;
lastPostion = this.transform.position;
}
private void getKeyboardMovment()
{
Vector3 inputValue = cam_movment.ReadValue<Vector2>();
inputValue.Normalize();
if( inputValue.sqrMagnitude > 0.1f)
{
targetPosition += inputValue;
}
}
private void updatePosition()
{
Debug.Log("is focusing? " + isFocusing);
if (isFocusing == true)
{
getFocusOffset();
transform.position += focus_offset;
}
if(targetPosition.sqrMagnitude > 0.1f)
{
speed = Mathf.Lerp(speed, maxSpeed, Time.deltaTime * acceleration);
transform.position += targetPosition * speed * Time.deltaTime;
}
else
{
movementVelocity = Vector3.Lerp(movementVelocity, Vector3.zero, Time.deltaTime * dampening);
transform.position += movementVelocity * Time.deltaTime;
}
targetPosition = Vector3.zero;
}
}
I can't really progress on with it without solving this bizzare bug, so any help you can must is extremely appreciated.
As far as this script is concerned, there is no issues and I'm able to get isFocusing bool as false and true through editor. Please check if you are setting this boolean value from anywhere else. Or if you can share that code.
I've done a find in files for the entire solution and it only found 5 mentions of "isfocusing", all of them are in that script
I was in the process of implementing a button to change this value, but that stopped prior to it. The buttom is set to use the core control gameobject (thing where this script is attachted to) but it doesn't run any functions.
Ok, I found part of the issue, the scripts keeps disabling itself when it the game runs
Answer by unity_LnG_A_HhXTvomQ · Mar 22 at 09:33 PM
I think I found what was wrong, there was another isntance of this script incorrectly attached a child of the camera rig, as well as bunch of setting stuff being put on the "onAwake" fuction, which was causing it to immediatly be disabled