- Home /
Problem with getkey/getkeydown
I have this script, and it works, and I want the camera to be in Third-Person mode when starting, and then in a certain position (First-Person view) when I press "A" once, but currently it starts in Third-Person, and it only goes to First-Person mode when I keep "A" pressed. I already tried Input.GetKeyDown, and it doesn't work, the it just works when I spam-click "A", I know it has something to do with that it checks every frame or something. Please help me!
Script (please ignore commented lines and the code for zooming and stuff, I just want the transition to work, Thanks! xoxo):
 using UnityEngine;
 using System.Collections;
 
 public class TPCamera : MonoBehaviour
 {
     //CAMERAS
     public Camera MainCamera;
     public Transform StartPos;
     public Transform EndPos;
     Transform CurrentPos;
 
     //ZOOMING
     public int MaximumZoomIn = 20;
     public int MaximumZoomOut = 100;
     public int ZoomSpeed = 5;
 
     //PANNING
     public float turnSpeed = 4.0f;    
 
     //OTHER
     public float Smooth = 5f;
 
     //PRIVATE
     private Vector3 mouseOrigin;        
     private bool isRotating;    
     Transform standardPos;            
 
     void Start()
     {
         standardPos = GameObject.Find ("CamPos").transform;
         //Find the current position of the camera
         CurrentPos = MainCamera.transform;
     }
     
     void Update ()
     {
         //CAMERAS
         //Sets the Camerastate to the opposite of the current Camerastate
         /*if(Input.GetKeyDown(KeyCode.Tab))
         {
             Camerastate = !Camerastate;
         }
         if(Camerastate == true)
         {
             transform.position = Vector3.Slerp(transform.position, ThirdPerson.transform.position, time * Smooth);
             transform.rotation = Quaternion.Slerp(transform.rotation, ThirdPerson.transform.rotation, time * Smooth);
             ThirdPerson.enabled = true;
             FirstPerson.enabled = false;
             Debug.Log("Third");
         }
         else
         {
             transform.position = Vector3.Slerp(transform.position, FirstPerson.transform.position, time * Smooth);
             transform.rotation = Quaternion.Slerp(transform.rotation, FirstPerson.transform.rotation, time * Smooth);
             ThirdPerson.enabled = false;
             FirstPerson.enabled = true;
             Debug.Log("First");
         }
         //Sets camerastate to the Third-Person when true, and First-Person when false
 
         ////////////////////////////////////////////////////////////////////////////////////////////
 */
         
         if(Input.GetKey(KeyCode.A))
         {
             TransitionOne();
             //transform.position = Vector3.Slerp(CurrentPos.transform.position, EndPos.transform.position, Time.deltaTime * Smooth);
             //transform.rotation = Quaternion.Slerp(CurrentPos.transform.rotation, EndPos.transform.rotation, Time.deltaTime * Smooth);
         }
 
 
 
 
         else
         {
             TransitionTwo();
             //transform.position = Vector3.Slerp(CurrentPos.transform.position, StartPos.transform.position, Time.deltaTime * Smooth);
             //transform.rotation = Quaternion.Slerp(CurrentPos.transform.rotation, StartPos.transform.rotation, Time.deltaTime * Smooth);
             //ThirdPerson();
         }
 
     }
 
     void TransitionOne()
     {
         transform.position = Vector3.Slerp(CurrentPos.transform.position, EndPos.transform.position, Time.deltaTime * Smooth);
         transform.rotation = Quaternion.Slerp(CurrentPos.transform.rotation, EndPos.transform.rotation, Time.deltaTime * Smooth);
     }
 
     void TransitionTwo()
     {
         transform.position = Vector3.Slerp(CurrentPos.transform.position, StartPos.transform.position, Time.deltaTime * Smooth);
         transform.rotation = Quaternion.Slerp(CurrentPos.transform.rotation, StartPos.transform.rotation, Time.deltaTime * Smooth);
         ThirdPerson();
     }
 
         void ThirdPerson()
         {
 
             transform.position = Vector3.Lerp(transform.position, standardPos.position, Time.deltaTime * Smooth);    
             transform.forward = Vector3.Lerp(transform.forward, standardPos.forward, Time.deltaTime * Smooth);
 
         // Zooming Out
         if (Input.GetAxis("Mouse ScrollWheel") < 0) 
         {
             if (Camera.main.fieldOfView<=MaximumZoomOut)
                 Camera.main.fieldOfView +=ZoomSpeed;
 
             if (Camera.main.orthographicSize<=20)
                 Camera.main.orthographicSize +=0.5f;
         }
         
         // Zooming In
         if (Input.GetAxis("Mouse ScrollWheel") > 0) 
         {
             
             if (Camera.main.fieldOfView>MaximumZoomIn)
                 Camera.main.fieldOfView -=ZoomSpeed;
             
             if (Camera.main.orthographicSize>=1)
                 Camera.main.orthographicSize -=0.5f;
         }
         
         //Switch Perspective and Orthograpic
         if (Input.GetKeyUp(KeyCode.O)) //Change "O" to whatever you want to change the camera switch input key.
         {
             
             if (Camera.main.orthographic==true)
                 Camera.main.orthographic=false;
             
             else
                 Camera.main.orthographic=true;
             
         }
 
         if(Input.GetMouseButtonDown(0))
         {
             // Get mouse origin
             mouseOrigin = Input.mousePosition;
             isRotating = true;
 
         }
 
 
         
         // Disable movements on button release
         if (!Input.GetMouseButton(0)) isRotating=false;
         
         // Rotate camera along X and Y axis
         if (isRotating)
         {
             Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
             
             transform.RotateAround(transform.position, transform.right, -pos.y * turnSpeed);
             transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed);
         }
 
     }
 }
Answer by xt-xylophone · May 19, 2014 at 04:22 AM
I havnt detailed all your code but I think I get what you're saying, the problem isnt with the functions, just your use of them.
getKey returns true WHILE it is held down. getKeyDown returns true for the FIRST frame it is held down.
So if you want essentially a toggle of first and third person, create a bool variable to save which one you are in, then use the code:
 if(Input.getKeyDown(KeyCode.A)) {
     firstPerson = !firstPerson;
 }
I made up the variable called firstPerson. How I would do it is if that is true, ill set the cam to first person mode, if its false, set to third person mode.
Hope you can take it from there!
Ow yes, I understand! I think that will do it, Thank you very much!
Ok, a quick update: I got it to work with your method, it works perfectly, thank you very much!! (And Congratz with 1k karma :p, you deserve it)
Your answer
 
 
             Follow this Question
Related Questions
Problem with Camera Transition (First-person to third-person, with Slerp) 1 Answer
c# script to move camera in unity by arrows ?? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Why do Two Instances of MonoDevelop Open when I double-click a CS file in Inspector? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                