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
1
Question by Yog0 · Oct 14, 2013 at 07:27 PM · javascriptdisablemouselook

Script to disable MouseLook?

I'm trying to disable MouseLook on my character controller using the following script, but I can't seem to get it to work. Any ideas?

 #pragma strict
 
 var IsMouselookOff : boolean = false ;
 
 
 function Update()
 {
     if(Input.GetKeyDown("F12")) {
         if (GameObject.Find("PlayerCamera").GetComponent(MouseLook).enabled == true){
             disableCamera();
             }
             }
    
      else if(Input.GetKeyDown("F12")){
             if (GameObject.Find("PlayerCamera").GetComponent(MouseLook).enabled == false){
                 enableCamera();
                 }
                 }
 }
  
 function disableCamera()
 {
     GameObject.Find("Player").GetComponent(MouseLook).enabled = false;
     GameObject.Find("PlayerCamera").GetComponent(MouseLook).enabled = false;
 }
 
 function enableCamera()
 {
     GameObject.Find("Player").GetComponent(MouseLook).enabled = true;
     GameObject.Find("PlayerCamera").GetComponent(MouseLook).enabled = 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

2 Replies

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

Answer by ArkaneX · Oct 14, 2013 at 07:58 PM

Assuming your First Person Controller is named Player and related camera is named PlayerCamera, then there are two bugs in your script:

1) Function key names must contain lowercase f letter, so in your case you have to use f12 instead of F12. Instead of string names, I suggest using KeyCode enumeration.

2) Having the same condition for if and then for else if will cause that code inside else if will never be called. I suggest following solution:

 #pragma strict
  
 private var _playerMouseLook : MouseLook;
 private var _playerCameraMouseLook : MouseLook;
 
 function Awake() {
     _playerMouseLook = GameObject.Find("Player").GetComponent(MouseLook);
     _playerCameraMouseLook = GameObject.Find("PlayerCamera").GetComponent(MouseLook);
 }
 
 function Update() {
     if(Input.GetKeyDown(KeyCode.F12)) {
         enableCamera(!_playerMouseLook.enabled);
     }
 }
  
 function enableCamera(enable : boolean) {
     _playerMouseLook.enabled = enable;
     _playerCameraMouseLook.enabled = enable;
 }


Comment
Add comment · Show 1 · 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 Yog0 · Oct 14, 2013 at 08:36 PM 0
Share

That worked! Thank you very much.

avatar image
0

Answer by DylanW · Oct 14, 2013 at 07:58 PM

I believe your problem is your if and else statement within function Update.

Try this instead:

 function Update()
 {
     if(Input.GetKeyDown("F12")) {
         if(GameObject.Find("PlayerCamera").GetComponent(MouseLook).enabled == true) {
             disableCamera();
         }
         else if(GameObject.Find("PlayerCamera").GetComponent(MouseLook).enabled == false) {
             enableCamera();
         }
     }
 }
Comment
Add comment · Show 1 · 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 Yog0 · Oct 14, 2013 at 08:36 PM 0
Share

Thanks for taking the time to answer!

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Cursor Lock to Center of screen script doesnt work please help. 2 Answers

Disable script from code 5 Answers

GuiTexture Width Change 1 Answer

Help needed with a pickup script 2 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