gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
Check if SimMGR gameobject exists before trying to change it. using System; using…
- User
Check if SimMGR gameobject exists before trying to change it.
using System; using UnityEngine;
public class FreeCam : MonoBehaviour { public Animator Warn; public GameObject SimMGR;
public bool CustomMode = false; public float movementSpeed = 33f; public float fastMovementSpeed = 233f; public float freeLookSensitivity = 3f; public float zoomSensitivity = 25f; public float fastZoomSensitivity = 50f; private bool looking = false; void Update() { // Determine movement speed based on shift key float currentMovementSpeed = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift) ? fastMovementSpeed : movementSpeed; // Handle camera movement HandleMovement(currentMovementSpeed); // Handle mouse look if (looking) { HandleMouseLook(); } // Handle zoom functionality HandleZoom(); // Handle free look activation HandleFreeLookActivation(); } private void HandleMovement(float speed) { Vector3 movement = Vector3.zero; if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) movement += transform.forward; if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) movement -= transform.forward; if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) movement -= transform.right; if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) movement += transform.right; if (Input.GetKey(KeyCode.Q)) movement += transform.up; if (Input.GetKey(KeyCode.E)) movement -= transform.up; if (Input.GetKey(KeyCode.R) || Input.GetKey(KeyCode.PageUp)) movement += Vector3.up; if (Input.GetKey(KeyCode.F) || Input.GetKey(KeyCode.PageDown)) movement -= Vector3.up; if (movement != Vector3.zero) { transform.position += movement.normalized * speed * Time.deltaTime; SimMGR.SetActive(false); Warn.Play("Warn_In"); } else { SimMGR.SetActive(true); Warn.Play("Warn_Out"); } } private void HandleMouseLook() { float newRotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * freeLookSensitivity; float newRotationY = transform.localEulerAngles.x - Input.GetAxis("Mouse Y") * freeLookSensitivity; transform.localEulerAngles = new Vector3(newRotationY, newRotationX, 0f); } private void HandleZoom() { float axis = Input.GetAxis("Mouse ScrollWheel"); if (axis != 0) { float zoomSensitivity = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift) ? fastZoomSensitivity : this.zoomSensitivity; transform.position += transform.forward * axis * zoomSensitivity; } } private void HandleFreeLookActivation() { if (!CustomMode) { if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.LeftAlt)) { SimMGR.SetActive(false); Warn.Play("Warn_In"); StartLooking(); } else if (Input.GetKeyUp(KeyCode.Space) || Input.GetKeyUp(KeyCode.LeftAlt)) { StopLooking(); SimMGR.SetActive(true); Warn.Play("Warn_Out"); } } else { if (Input.GetKeyDown(KeyCode.Mouse2)) StartLooking(); else if (Input.GetKeyUp(KeyCode.Mouse2)) StopLooking(); } } void OnDisable() { StopLooking(); } public void StartLooking() { looking = true; Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; } public void StopLooking() { looking = false; Cursor.visible = true; Cursor.lockState = CursorLockMode.None; } - Assistant