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 oinkoinkflapflap · Jan 31, 2011 at 08:54 PM · javascriptdisablemouselookenabledpausing

disabling a script (MouseLook)

when pausing my mouse look script is still active, how do i disable it, i tried using this script and butting it on the same object with the script attached but nothing...

if (Input.GetButtonUp("Esc"))
GetComponent("MouseLook").enabled = false;
else if (Input.GetButtonUp("Esc"))
GetComponent("MouseLook").enabled = true;

Thanks alot :D

Comment
Add comment · Show 1
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 Flickayy · Apr 21, 2015 at 08:34 PM 0
Share

How about trying this.

 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Esc)) {
        GetComponent<$$anonymous$$ouseLook>().enabled = false;
 }

See if that works, then we can build on the logic.

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tenukii · Apr 14, 2014 at 05:19 PM

Example code for a main menu that toggles on escape key and turns off mouse look in C#. Of course you have to traverse whatever object hierarchy you have, not just blindly copy/paste. ps: Their are two mouse look scripts, one for the player and one for the player camera, the reason is you don't want the player to rotate in the up/down direction, just rotate around the y axis - so the scripts are slightly different.

 using UnityEngine;
 using System.Collections;
 
 public class MainMenu : MonoBehaviour
 {
     public bool displayMenu = true;
     public Texture mainMenuImage;
     MouseLook playerLook;
     MouseLook playerCameraLook;
     Rect windowRect;
     // Use this for initialization
     void Start ()
     {
         playerLook = (MouseLook)GameObject.Find ("First Person Controller").GetComponent ("MouseLook");
         playerCameraLook = (MouseLook)GameObject.Find ("Main Camera").GetComponent ("MouseLook");
         playerLook.enabled = !displayMenu;
         playerCameraLook.enabled = !displayMenu;
     }
     // Update is called once per frame
     void Update ()
     {
         if (Input.GetKeyDown (KeyCode.Escape)) {
             displayMenu = !displayMenu;
             playerLook.enabled = !playerLook.enabled;
             playerCameraLook.enabled = !playerCameraLook.enabled;
         }
     }
 
     void OnGUI ()
     {
         if (displayMenu) {
             windowRect = GUI.Window (0, new Rect (Screen.width / 4, Screen.height / 4, 2 * Screen.width / 4, 2 * Screen.height / 4), DrawMainMenu, "Ambient Main");
         }
     }
 
     void DrawMainMenu (int windowID)
     {
         GUILayout.Label (mainMenuImage);
         if (GUILayout.Button ("Quit")) {
             Application.Quit ();
         }
     }
 }
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 kennypu · Jan 31, 2011 at 11:25 PM

this would be better:

if(Input.GetKeyUp(KeyCode.Escape))
  GetComponent("MouseLook").enabled = !GetComponent("MouseLook").enabled

what that does is it'll flip the true and false. so if its true, it'll be come false, and vice versa. Your script didn't wprk because both of those statements will be true all the time. It's like asking, if 1 ==1, do this, else if 1 == 1, do this. both are true, so in the end, it'll just be set to enabled again.

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 Jessy · Jan 31, 2011 at 11:30 PM 0
Share

This answer was posted while I was posting. It's the same thing, but isn't as performant.

avatar image oinkoinkflapflap · Feb 01, 2011 at 04:07 PM 0
Share

sorry guys but neither of these scripts seem to work, were do i attatch them?

avatar image kennypu · Feb 04, 2011 at 10:10 PM 0
Share

it should be attached to whatever object that has the mouselook script. $$anonymous$$ake sure you put in the Update function

avatar image
0

Answer by Jessy · Jan 31, 2011 at 11:29 PM

There's not much that's right about that script. Try this instead, and let us know if that's what you're talking about.

function Update () {
    if (Input.GetKeyUp(KeyCode.Escape)) {
        var mouseLook = GetComponent.<MouseLook>();
        mouseLook.enabled = !mouseLook.enabled;
    }
}
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 kennypu · Feb 01, 2011 at 12:34 AM 0
Share

I don't see how yours is more performant is it is the same thing.

avatar image Jessy · Feb 01, 2011 at 01:00 AM 0
Share
  1. Two GetComponent calls.

  2. Inefficient form of GetComponent. (string vs. generic)

avatar image oinkoinkflapflap · Feb 01, 2011 at 04:07 PM 0
Share

sorry guys but neither of these scripts seem to work, were do i attatch them?

avatar image
0

Answer by IBDelta · May 10, 2011 at 03:29 AM

I think what these fine people fail to realize is that the FPS controller has two MouseLook scripts attached, one only appears to control the vertical for some strange reason I may never fully understand. No offence intended, I'm no programmer, but we can all miss the woods for the trees on occasion. My solution was to edit the MouseLook script itself with a simple toggling boolean. The main body of the code was then wrapped in an if statement that only executes the code when boolean is true. You can change the Input.GetKey to whatever floats your boat :-)

Replace the entire contents of the MouseLook script with the following code and the looking behavior is toggled with the "m" key.

using UnityEngine; using System.Collections;

 /// MouseLook rotates the transform based on the mouse delta.
 /// Minimum and Maximum values can be used to constrain the possible rotation

 /// To make an FPS style character:
 /// - Create a capsule.
 /// - Add the MouseLook script to the capsule.
 ///   -&gt; Set the mouse look to use LookX. (You want to only turn character but not tilt it)
 /// - Add FPSInputController script to the capsule
 ///   -&gt; A CharacterMotor and a CharacterController component will be automatically added.

 /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
 /// - Add a MouseLook script to the camera.
 ///   -&gt; Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
 [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;

     bool MouseActive=true;

     void Update ()
     {       
         if(Input.GetKeyDown("m"))
         {
             MouseActive = !MouseActive;
         }
         if(MouseActive)
         {
             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;
     }
 }

Good Luck!

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 goldkillerv · Nov 07, 2012 at 12:52 PM

 function Update()
 
 {
 
     if (Input.GetKeyDown ("Esc"))
 
     {
 
         GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = false;
 
     }
 
     else
 
     {
 
         GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = true;
 
 }

I am pretty sure that this will work.

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 xwheat32x · Mar 03, 2014 at 08:49 PM 0
Share

GameObject.Find("First Person Controller").GetComponent($$anonymous$$ouseLook).enabled = false;

Isn't quite correct. It has to be:

GameObject.Find("First Person Controller").GetComponent("$$anonymous$$ouseLook").enabled = false;

avatar image robertbu · Mar 03, 2014 at 08:53 PM 0
Share

@xwheat32x - your solution will not work. GetComponent() with a string returns a Component. A Component does not have an 'enabled' flag. The one you are trying to correct should work if the name in the Find() is a perfect match to the one-and-only object in the scene that has that exact name.

avatar image BMRX · Apr 21, 2015 at 07:55 PM 0
Share
 GameObject.Find("First Person Controller").GetComponent("$$anonymous$$ouseLook").enabled = false; 

Unity no longer understands what this means... I don't get it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Disable all instances of a component 2 Answers

Script to disable MouseLook? 2 Answers

How to Disable Mouse and Character movement? (Javascript) 1 Answer

Unable to disable the MouseLook script 2 Answers

Disable script from code 5 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