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 Ouija · Nov 30, 2012 at 04:53 PM · camerapause

Mouse Look Script Pause

Hey guys, I'm using the "mouse look" script. I'll paste it here, its only tiny. I'm sure most fo you have seen it. So what I wanted to do with it is, When I press, Either 1 or 2, for the script to pause? or perhaps deactivate. And then most likely activate again when pressed again. Basically I want a way to stop is on keypress :) Any ideas??

 [AddComponentMenu("Camera-Control/Mouse Look")]
 public class MouseLook : MonoBehaviour {
 
     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
     public RotationAxes axes = RotationAxes.MouseXAndY;
     public float sensitivityX = 15F;
     public float sensitivityY = 15F;
 
     public float minimumX = -360F;
     public float maximumX = 360F;
 
     public float minimumY = -60F;
     public float maximumY = 60F;
 
     float rotationY = 0F;
 
     void Update ()
     {
         if (axes == RotationAxes.MouseXAndY)
         {
             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
             
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             
             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
         }
         else if (axes == RotationAxes.MouseX)
         {
             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
         }
         else
         {
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             
             transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
         }
     }
     
     void Start ()
     {
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
 }
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

3 Replies

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

Answer by Ouija · Nov 30, 2012 at 09:59 PM

This is what I got, I tired to use comment, but the code gets messy?

     private bool isEnabled = true;
 
 void Update()
 {    
     //KeyReleased
     if (!Input.GetKeyUp(KeyCode.A))
     {
         //Toggle the enabled flag
         isEnabled = !isEnabled;
     }
 
     if (!isEnabled)
         return;
         
         
     
         if (axes == RotationAxes.MouseXAndY)
         {
             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
             
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             
             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
         }
         else if (axes == RotationAxes.MouseX)
         {
             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
         }
         else
         {
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             
             transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
         }
     }
     
     void Start ()
     {
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }    
         
         
 
 }



Camera starts fine but Nothing seems to happen when I press the A key. The camera still moves around freely. I must be missing something LOL

Comment
Add comment · Show 4 · 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 kaolas · Nov 30, 2012 at 11:53 PM 0
Share

That's my fault. I had a typeo in my answer edit above. Change the " if (!Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A)) " - get rid of the ! so that it reads:

if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A))

Basically right now you're checking if the A key is NOT released, which was my typeo. Sorry for the confusion.

avatar image Ouija · Dec 01, 2012 at 12:16 AM 0
Share

Very Cool! it works like a charm thanks alot :) We can edit this post so it has the right answer if you wish

avatar image Ouija · Dec 01, 2012 at 06:49 PM 0
Share

Hey kaolas last night I stumbled upon something I never thought about lol I have it set up so when you press either button liek we discussed, but there is a problem One button makes a menu come up, and the second button makes a second menu come up

so you end up getting stuck in a loop when toggling the button, ex. I press 1 and the menu comes up, it can close fine if you press 1

but when you press 1, and then 2 it starts to get messy I can try to explain better if needed!

avatar image kaolas · Dec 04, 2012 at 03:57 PM 0
Share

Added a response at the bottom of my answer above. Let me know if it doesn't make sense.

avatar image
0

Answer by ShadowAngel · Nov 30, 2012 at 06:06 PM

evry component has .active parameter. so just use something like:

 gameObject.GetComponent(MouseLook).active = false;
Comment
Add comment · 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
0

Answer by kaolas · Nov 30, 2012 at 06:13 PM

If I understand your question correctly, you want to "shortcut" the Update() function when a key is pressed.

To do that, add this at the beginning of your Update():

 if (Input.GetKey(KeyCode.Keypad1) || Input.GetKey(KeyCode.Keypad2))
    return;

This will cause the Update() to stop processing so long as either the 1 or 2 keys are held down. If you want to do a toggle, rather than a hold, you would need to look into the Input.GetKeyDown() and Input.GetKeyUp() functions and based on those, define when you toggle the update on or off.

EDIT (response to comment):

The Input.GetKey() will only tell you if a key is currently being pressed. If you want it to be on, then you press a key and it goes to off, then press a key and it goes back to on, that's the toggle situation I described.

Here are the "events" you can hook into:

 void Update()
 {
     //KeyPressed
     if (Input.GetKeyDown(KeyCode.Keypad1))
     {
         //The user has just now pressed the key down.  
     }
 
     //KeyHeldDown
     if (Input.GetKey(KeyCode.Keypad1))
     {
         //The user is currently holding the key down.  
     }
     
     //KeyReleased
     if (Input.GetKeyUp(KeyCode.Keypad1))
     {
         //The user has just now released the key.  
     }
 }

What you will most likely want to do is hook into the "KeyReleased" functionality and toggle the value of whether you should process the update or not:

 private bool isEnabled = true;
     
 void Update()
 {    
     //KeyReleased
     if (Input.GetKeyUp(KeyCode.Keypad1))
     {
         //Toggle the enabled flag
         isEnabled = !isEnabled;
     }
     
     if (!isEnabled)
         return;
     
     ... the rest of your update code ...
 }

This will default the update to be on because isEnabled is true when it is initialized, then let you press a key to toggle it on or off (it just negates its prior value).

The more you get into Input and want finer control over what you're doing with it, the more I would recommend existing plugins like FingerGestures. There's no reason to reinvent the wheel and this and other plugins like it do a very good job of encapsulating the input events.

EDIT: Fixed typeos in the GetKeyUp() checks. EDIT: Response to comment:

If you want two buttons to toggle two pieces of functionality, you should have two different booleans to track the state of each of those pieces of functionality.

 //Functionality1
 if (!Input.GetKeyUp(KeyCode.A))
 {
     //Toggle the enabled flag
     isFunction1Enabled = !isFunction1Enabled;
 }
 
 //Functionality2
 if (!Input.GetKeyUp(KeyCode.B))
 {
     //Toggle the enabled flag
     isFunction2Enabled = !isFunction2Enabled;
 }
 
 if (isFunction1Enabled)
 {
     //Do functionality 1
 }
 
 if (isFunction2Enabled)
 {
     //Do functionality 2
 }

This will let you independently toggle each functionality without respect for the toggle state of the other functionality. You're no longer shortcutting the Update(), but rather controlling when things happen or don't happen. Of course, if both are false, your Update() won't do anything.

Comment
Add comment · Show 2 · 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 Ouija · Nov 30, 2012 at 07:29 PM 0
Share

Thanks for the answers! $$anonymous$$aolas I believe you are on the right track, only thing is Id like the "look" script to be on as default , just given the option to turn it on and off afterwards with the 1 or 2 key Sorry for leaving out that bit of info!

avatar image Ouija · Nov 30, 2012 at 09:58 PM 0
Share

hmm. I think I wrote it out correct. but the camera still seems to move freely

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

13 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

Related Questions

How to deactivate a script temporarly on another object when clicking a GUI ? 2 Answers

question about camera limits to the screen 0 Answers

Turn Off/on culling mask by script? 2 Answers

Camerascript works strange! 0 Answers

Editing the standard FPS to work different 0 Answers


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