This question was
closed Dec 02, 2017 at 08:54 AM by
1sd15 for the following reason:
The question is answered, right answer was accepted
InputKey on Update with INT Counter - Camera Switch - Not working properly.
The Problem:
So, when I Input a Key in the void update, it repeats the input multiple times on one press(I Think) and it repeats the addition of 1 to the INT multiple times as well. I have no idea why or if this is even the problem! I just wanted to make a simple camera switch script that would change the object enabled based on inputs like a flip-flop. :|
When I press the key in question, instead of going to the next script it bugs out the camera a bit as it spams the input. I think that is what is happening as when I print the counter, it goes wild through all of the counts and bugs the screen out a bit as it goes from Camera 1 to Camera 3, to Camera 2 randomly.
Anyway, I am sorry if that does not make much sense, it's very hard to explain this kind of problem as it only really appears on the unity side of things rather then on the coding side. Thanks for your help in advance! I have put below my code and a GIF of the problem, any feedback, or queries on my question is welcomed and I hope the way I have laid it out suits you. :)
Code:
using UnityEngine;
using System.Collections;
public class CameraSwitch : MonoBehaviour {
public GameObject FirstPerson;
public GameObject ThirdPersonFixed;
public GameObject ThirdPersonRotation;
private int CKeyCounter = 0;
void Update () {
//Input
if (Input.GetKey("c"))
{
//InputCounter
CKeyCounter = CKeyCounter + 1;
//ResetCounter
if (CKeyCounter == 4)
{
CKeyCounter = 1;
}
//Inputs
if (CKeyCounter == 1)
{
FirstPerson.SetActive(false);
ThirdPersonRotation.SetActive(true);
}
if (CKeyCounter == 2)
{
ThirdPersonRotation.SetActive(false);
ThirdPersonFixed.SetActive(true);
}
if (CKeyCounter == 3)
{
ThirdPersonFixed.SetActive(false);
FirstPerson.SetActive(true);
}
// Small Hierarchy Set:
//
// 1 = First Person
// 2 = Third Person Fixed
// 3 = Third Person Rotation
}
//TestingPrinter
print(CKeyCounter);
//VeryUseful
}
}
GIF Picture
Comment
Best Answer
Answer by coolyer · Nov 01, 2017 at 11:23 PM
The problem is that you set Input.GetKey("c"))
Change it to Input.GetKeyDown("c"))
To make this work you need to create a empty game object called "Manager" or somthing along the lines, then put the "CameraSwitch" script in the Manager Object you made. And it should work.
Follow this Question
Related Questions
Weird Rendering Unity5 1 Answer
Switching player controllers 0 Answers
Transforms update problem 0 Answers
question about unity version update 0 Answers
Hello.I use the Region Capture to create the coloring of augmented reality. 0 Answers