Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alexanderameye · May 18, 2014 at 07:27 PM · c#cameramonodevelop

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);
         }
 
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alexanderameye · May 19, 2014 at 05:16 AM 1
Share

Ow yes, I understand! I think that will do it, Thank you very much!

avatar image alexanderameye · May 19, 2014 at 05:29 AM 1
Share

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)

avatar image xt-xylophone · May 19, 2014 at 09:27 AM 0
Share

Glad it worked out :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Smooth transition from Camera1 to Camera2? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges