Why are my keys being registered multiple times?
So I've been working on a multiple-camera system. 1 = Main Camera 2 = First Person 3 = Third Person When I press a key, the console is reporting my Log as pressing key(s) (each key I press) 4 times.
I've been able to get this camera switch to work two times (I've had to make this game three times due to losing my computers and sending them in for repairs). For some reason it does not want to cooperate this time.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public Camera MainCamera; // Freeform camera (Panable around the character)
public Camera FirstPerson; // First Person Perspective
public Camera ThirdPerson; // Third Person Camera (Over the shoulder)
public GameObject player; // Player reference object
private Vector3 offset; // Offset Vector for distance from the player
// Use this for initialization
void Start ()
{
// Camera default settings
#region
MainCamera = GetComponent<Camera>();
FirstPerson = GetComponent<Camera>();
ThirdPerson = GetComponent<Camera>();
MainCamera.enabled = true;
FirstPerson.enabled = false;
ThirdPerson.enabled = false;
#endregion
// Offset is equal to the transform position minus the player's transform position
offset = transform.position - player.transform.position;
}
// Update is called once per frame
void Update ()
{
// Camera Follows the Player
#region
// Camera will transform its position relative to the player's position and the offset
transform.position = player.transform.position + offset;
#endregion
// Main Camera (Freelow)
if (Input.GetKey(KeyCode.Alpha1)) // Press of the 1 key
{
Debug.Log( "Main Camera Enabled, 1 Key pressed." );
MainCamera.enabled = MainCamera.enabled;
FirstPerson.enabled = !FirstPerson.enabled;
ThirdPerson.enabled = !ThirdPerson.enabled;
}
// First Person Camera
if (Input.GetKey(KeyCode.Alpha2))
{
Debug.Log( "First Person Camera Enabled, 2 Key pressed." );
FirstPerson.enabled = FirstPerson.enabled;
MainCamera.enabled = !MainCamera.enabled;
ThirdPerson.enabled = !ThirdPerson.enabled;
}
// Third Person Camera
if (Input.GetKey(KeyCode.Alpha3))
{
Debug.Log( " Third Person Camera Enabled, 3 key pressed." );
ThirdPerson.enabled = ThirdPerson.enabled; // Enable the Third Person Camera
MainCamera.enabled = !MainCamera.enabled; // Disable the Main Camera
FirstPerson.enabled = !FirstPerson.enabled; // Disable the First Person Camera
}
}
}
I thought that maybe the regions of code I had these stored in were affecting them but that doesn't seem to be the case. I've tried making these cameras change to simple false instead of !Camera.enabled (toggled) so see if there's a difference and it just makes it so no full screen camera is active.
Thanks in advance. This is driving me nuts! If there's an easier or 'better' way to do this I'd love to know.
Answer by JoeVRoh · Jul 13, 2016 at 11:19 PM
By removing the toggles (!Camera.enabled) I've alleviated the cameras switching from each other too fast to see.
My keys are still being pressed 4 times each though. Would it be better to use FixedUpdate or LateUpdate?
Also, what about GetKeyDown or Up or just GetKey? I'm attempting to understand these differences. That's why I find the core issue is a key being processed multiple times within the update.
Your answer
Follow this Question
Related Questions
Switch between 5 cameras in the game by clicking. 0 Answers
How can I get all cameras enabled true false states ? 0 Answers
Open door by completing all puzzles 1 Answer
CAMERAS ERROR , I NEED HELP 0 Answers