• 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 gordon230883 · Sep 21, 2011 at 02:46 PM · mousedisableweapon

disable mouse look and weapon

ok so i have a code door and when i walk up to the door a gui code panel appears for me to enter the code to unlock it but the camera keeps moving along with the mouse and when i click the open button my gun fires. Here is what i have so far, the mouse just doesn't stop.

 var isCamPaused : boolean ;

function OnTriggerEnter(collision: Collider){

 isCamPaused = true;

}

function OnTriggerExit(collision: Collider){

 isCamPaused = false;

}

function Awake(){

 if(isCamPaused){
         isCamPaused = true;
         GameObject.Find("First Person Controller").GetComponent("MouseLook").enabled = false;

         GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = false;
         }

}

Comment
Add comment · Show 8
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 Kacer · Sep 21, 2011 at 04:02 PM 0
Share

please format your code properly :)

avatar image gordon230883 · Sep 21, 2011 at 11:38 PM 0
Share

sorry was in a rush earlier hope this is easier to read.

avatar image john-essy · Sep 21, 2011 at 11:43 PM 0
Share

Press the 01010 button and put your code in there Thanks The i will tell you

avatar image gordon230883 · Sep 21, 2011 at 11:47 PM 0
Share

you may be able to tell that im not a coder but trying my best to pick it up.

avatar image john-essy · Sep 21, 2011 at 11:49 PM 0
Share

I know mate it is hard but your code is really unreadable edit the code and press the 101010 button then put your code in there. Otherwise people on here will learn that you don't listen to advice and wont help you mate, Happened to me:(

Show more comments

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by john-essy · Sep 21, 2011 at 11:53 PM

Here is a fix for your problem Mate, There was one major thing you where doing wrong you where saying OnTriggerEnter But you where not saying what you collide with to set the trigger

var isCamPaused : boolean = false;

function Awake()

{

 isCamPaused = false;

 if(isCamPaused == true)

     {

         gameObject.Find("FirstPersonController").GetComponent("MouseLook")

.enabled = false;

     }

}

function OnTriggerEnter(hit : Collider)

{

 if(hit.GameObject.name == "YourTrigger")

 {

     isCamPaused = true;

 }

}

function OnTriggerExit(unhit: Collider)

{

 if(unhit.gameObject.name == "YourTrigger")

     {

         isCamPaused = false;

     }

}

Comment
Add comment · Show 5 · 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 gordon230883 · Sep 22, 2011 at 12:13 AM 0
Share

i set a box collider as trigger on the same item and if i watch the inspector window whilst running my game when i hit the collider the (isCamPaused boolean does change to true but the mouse still works so i dont think anything is wrong with the trigger part as that seems to be the only working part haha. but im only a beginner so i may be totally wrong i will try this code.

avatar image gordon230883 · Sep 22, 2011 at 08:18 AM 0
Share

Can anyone think of anything else? my trigger is working but the mouse doesn't deactivate

avatar image john-essy · Sep 22, 2011 at 10:12 AM 0
Share

$$anonymous$$aybe it could be your FPS controller rename it so that there are no spaces in the inspector as with most fps controllers they are spelt with spaces do the below

First Person Controller --> To this FirstPersonController

avatar image gordon230883 · Sep 22, 2011 at 10:28 AM 0
Share

I tried that but the mouse look script is still not turning off.

avatar image gordon230883 · Sep 22, 2011 at 10:50 AM 0
Share

ok just to make it clear i have this script attached to a door and need the cam to stop as a gui code panel pops up to enter a key code to open the door. what im trying to say is because the code isnt attached to the First Person Controller will if be able to find the $$anonymous$$ouse Look script

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

does this part of the script search for the component everywhere or does the script only search the item it is attached to? dont know if this is very clear im just thinking because the script isnt attached to the FPC will it find a component attached to the FPC haha im a noob incase you have not already guessed so still not quite sure how everything works.

avatar image
0

Answer by Tommynator · Sep 22, 2011 at 10:31 AM

You are using Awake in the wrong sense. Awake is called internally by the Unity Runtime Environment exactly 1 time, when the component is initialized.

You want to wrap your logic into another function and call this from your OnTriggerEnter() function like this:

 function OnTriggerEnter(collision: Collider)
 {
     disableCamera();
 }

 function disableCamera()
 {
     GameObject.Find("First Person Controller").GetComponent("MouseLook").enabled = false;
     GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = false;
 }

And btw, you are doing yourself a favour if you stop using GameObject.Find() and instead use fix references to any external objects. Also better use the generic version of GetComponent for type safety.

Cheers, Tommy

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 john-essy · Sep 22, 2011 at 12:03 PM 0
Share

Thanks for that $$anonymous$$my could you elaborate anything that will help with performance or what not i want to know lol :)

avatar image
0

Answer by gordon230883 · Sep 22, 2011 at 11:53 AM

ok just to make it clear i have this script attached to a door and need the cam to stop as a gui code panel pops up to enter a key code to open the door. what im trying to say is because the code isnt attached to the First Person Controller will if be able to find the Mouse Look script

 GameObject.Find("First Person Controller").GetComponent("MouseLook").enabled = false;

does this part of the script search for the component everywhere or does the script only search the item it is attached to? dont know if this is very clear im just thinking because the script isnt attached to the FPC will it find a component attached to the FPC haha im a noob incase you have not already guessed so still not quite sure how everything 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 john-essy · Sep 22, 2011 at 12:05 PM

Here is a part from my game i am making it works perfect

 mouseLook = gameObject.Find("Camera").GetComponent("MouseLook").enabled = true; // find the mouselook script and enable it for viweing FPS Style
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 gordon230883 · Sep 22, 2011 at 12:24 PM 0
Share

is this script attached to your FPC

avatar image gordon230883 · Sep 22, 2011 at 12:34 PM 0
Share

say i have this script attached to a cube with a box collider trigger and when i touch the cube i want it to freeze the FPC mouse look. should it work? the script is asking to disable the FPC mouse look even tho the script is not attached to the FPC is this allowed?

 function OnTriggerEnter(collision: Collider){


     paused();

 }

 function OnTriggerExit(collision: Collider){

     unpaused();

 }

 function paused()
 {
         
         GameObject.Find("First Person Controller").GetComponent("$$anonymous$$ouse Look").enabled = false;
         GameObject.Find("$$anonymous$$ain Camera").GetComponent("$$anonymous$$ouseLook").enabled = false;
   }

function unpaused() {

         GameObject.Find("First Person Controller").GetComponent("$$anonymous$$ouse Look").enabled = true;
         GameObject.Find("$$anonymous$$ain Camera").GetComponent("$$anonymous$$ouseLook").enabled = true;
   }
avatar image
0

Answer by john-essy · Sep 22, 2011 at 03:43 PM

I have never seen function

 OnTriggerEnter(collision: Collider){
 
 
     paused();
 
 }
 

    i would do this 
 function OnTriggerEnter(hit : Collider)
 {
     if(hit.gameObject.tag == player)
         {

GameObject.Find("First Person Controller").GetComponent("Mouse Look").enabled = false;

GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = false;

         }
 
 
 }
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 gordon230883 · Sep 22, 2011 at 10:42 PM 0
Share

didnt work either :( why oh why is it not working! lol

avatar image gordon230883 · Sep 23, 2011 at 12:57 PM 0
Share

my code is in java and the code im trying to disable is C# does this make a difference? getting so stressed about this one been trying for 3 days to get it to work now. Any help is appreciated

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Disable/enable script and animation when you move your mouse cursor 1 Answer

Weapon selection system 2 Answers

Fire weapon from turret to mouse position (3D Isometric shooter) 1 Answer

Suspend Mouse Look 2 Answers

How can i disable left button mouse, when i click on GameObject? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges