Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 tomazsaraiva · Mar 18, 2010 at 06:53 PM · guilock-cursormouse-look

Locking/unlocking cursor

is it possible to disable the mouse look script applied to a camera?

The problems is that I have several GUI buttons that I need to use, but I'm using a FPS camera with the mouse look and cursor lock scripts applied to it.

I want to be able to unlock the cursor, use the GUI buttons and then lock the cursor again. While the cursor is unlocked I want the camera to stop moving with the movement of the mouse.

Any thoughts?

Comment
Add comment · Show 2
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 Lipis · Mar 18, 2010 at 07:10 PM 0
Share

$$anonymous$$y guess is that you are using the First Person Controller prefab from Standard Assets... correct?

avatar image tomazsaraiva · Mar 18, 2010 at 07:34 PM 0
Share

@Lipis yes..not exactly the same but with the same configuration

4 Replies

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

Answer by Lipis · Mar 18, 2010 at 07:37 PM

Since you are using the First Person Controller prefab, then you don't have the MouseLook component only on the Main Camera but also on the Player (to be able to turn). So you want to disable both of them in order stop the movement.

Here is an example on how you can disable/enable Mouse Look components of your player. Create a new Javascript, copy/paste this code to it and attach it to the First Person Controller.

private var mouseLook: MouseLook[]; private var canLook: boolean = true;

function Start() { mouseLook = gameObject.GetComponentsInChildren(MouseLook); Screen.lockCursor = true; }

function Update () { if (Input.GetKeyDown(KeyCode.Escape)) { if (canLook) { disableMouseLook(); } else { enableMouseLook();
} } }

function enableMouseLook() { canLook = true; Screen.lockCursor = true; for (var look in mouseLook) { look.enabled = true; } }

function disableMouseLook() { canLook = false; Screen.lockCursor = false; for (var look in mouseLook) { look.enabled = false; } }

The above example disables and enables the Mouse Look when you are hitting the Esc key. Feel free to ask for more details in the comments if necessary.

Comment
Add comment · Show 14 · 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 tomazsaraiva · Mar 18, 2010 at 08:01 PM 0
Share

wow thats a lot..this is exactly what I was looking for..just one question..how can I lock the cursor when I activate the mouse look?

avatar image Eric5h5 · Mar 18, 2010 at 08:02 PM 0
Share

Better to just toggle the enabled value; you can get rid of most of that code: "if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape)) for (look in mouseLook) look.enabled = !look.enabled;" Also use "private var mouseLook : $$anonymous$$ouseLook[];" to avoid slow dynamic typing.

avatar image Lipis · Mar 18, 2010 at 08:09 PM 0
Share

@maveryck21 I updated the code with the lockCursor included. @Eric5h5 I'm aware of that.. and I was thinking of doing that but toggling the the $$anonymous$$ouseLook but this will be only useful in this example with the Escape... Obviously maveryck21 would like to use it in a more complex way :) Hopefully...

avatar image Lipis · Mar 18, 2010 at 08:42 PM 1
Share

@maveryck21 the web player is a different case especially with the Escape key... more details here: http://unity3d.com/support/documentation/ScriptReference/Screen-lockCursor.html

avatar image alexnode · Mar 26, 2010 at 05:31 PM 1
Share

I got this message when I saved it ... it doesn't like the mouselook =game.Object... at line five ... any suggestions ? i have a set up with a far and near camera but i use the default scripts.
InvalidCastException: Cannot cast from source type to destination type. DisableEnable$$anonymous$$ouseLook.Start () (at Assets\Scripts\DisableEnable$$anonymous$$ouseLook.js:5)

Show more comments
avatar image
0

Answer by Simonced · May 06, 2011 at 02:07 AM

Hi guys,

I found your solution and it helps me, but I still encounter an issue. I first had to change the beginning part of the script to :

private var mouseLook: Array = new Array(); private var canLook: boolean = true;

function Start() {

 for(var mouseScriptFound : MouseLook in gameObject.GetComponentsInChildren(MouseLook) ) {
     mouseLook.Add( mouseScriptFound );
 }

 Screen.lockCursor = true;

}

Then everything is ok, BUT, I have the same issue than when I tried my own script. One I hit Escape, the cursor unlocks and shows off, but if I hit ESC again, even if the mouseLook scripts are activated again, the mouse cursor still shows off.

I'm using Unity 3.3. Does anyone one know what's happening? Thanks for unlightning me ;)

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 AVLead · Apr 13, 2012 at 05:08 AM

@Simonced Maybe you have have find, but it can be useful for others... Try with another key than escape, for me, that works :)

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 ariel-terrani · May 16, 2016 at 04:55 AM

in your Player Script: [CODE] public GameObject menu; public static bool menuActive; void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { menu.SetActive(!(Screen.lockCursor = !(menuActive = !menuActive))); return; } } void Start() { Screen.lockCursor = true; } [/CODE] In your Camera Script:

 void Update()
 {
     if (!Player.menuActive)
     {

//your stuff here

} }

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Cursor not displaying in the Inventory window 0 Answers

Can't use camera.main [SOLVED] 1 Answer

How do i disable mouselook when a button is pressed? 2 Answers

in 4.6RC1 uGUI, sprites blocks buttons/scrollrect from clicking/scrolling 2 Answers

cut scence 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