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 eshonbel · May 01, 2014 at 06:00 PM · guimobileoptimizationwindows phoneaimdownsights

Aim Down Sight Script not working with GUI Button, but working with MouseButtonDown(0)

I am making a fps game for windows phone. I have an Aim Down Sight script for my gun. It works fine with Input.GetMouseButton, but when I try and use the GUI function instead, it draws it but does nothing. Here is the original script: #pragma strict

 var Aim : boolean = false; var Cam : GameObject;
 
 
 function Update () { if(Input.GetMouseButtonDown(0)){ Aim = true; if(Aim == true){ Cam.active = true; } }
 
 if(Input.GetMouseButtonUp(0)){ Aim = true; if(Aim){ Cam.active = false; }
 }
 }
 

Here is the script with GUI Functions:

 enter code here#pragma strict
 
 var Aim : boolean = false; var Cam : GameObject;
 
 
 function OnGUI () { if(GUI.Button (Rect (20,40,80,20), "ADS")){ 
 Aim = true; if(Aim == true){ Cam.active = true; } }
 
 if(GUI.Button (Rect (20,40,80,20), "ADS")){
  Aim = true; if(Aim){ Cam.active = false; }
 }
 }

I am much less confident with GUI input. I keep pressing it but it doesn't ADS like the MouseButton(0). I'd much rather have this as a GUI functionality, as it plays weird on my phone as a tap.

Thanks. BenDy Games, Game Development For Windows Phone 8

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 eshonbel · May 03, 2014 at 01:31 PM 0
Share

Please may someone help? I was thinking of $$anonymous$$ouseButtonUp for GUI, but there is none. I really need this, I will definetly upvote the best answer!

1 Reply

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

Answer by robertbu · May 03, 2014 at 02:14 PM

By convention, variables should start with a lower case letter. Functions and classes start with an upper case letter. Here is a bit of code that does with GUI what your original code was doing with OnMouseDown()/OnMouseUp():

 #pragma strict
  
 var aim : boolean = false; 
 var cam : GameObject;
 
 private var rect = Rect(20,40,80,20);
  
 function OnGUI () { 
 
     var e = Event.current;
 
     if (e.type == EventType.MouseDown && rect.Contains(e.mousePosition)) {
         aim = true;
         cam.SetActive(true);
         Debug.Log("Finger Down");
     }
 
     if (e.type == EventType.MouseUp && rect.Contains(e.mousePosition)) {
         aim = true;
         cam.SetActive(false);
         Debug.Log("Finger Up");
     }
     
         GUI.Button (Rect (20,40,80,20), "ADS");
 }

Note I was a bit confused about 'aim' being set to 'true' for both the mouse down and the mouse up case. Are you sure that is what you want to do? Is there more to this script?

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 eshonbel · May 07, 2014 at 07:11 AM 0
Share

Thanks so much, it worked perfectly ;) The script is pretty small, but yeah, that's what I wanted to do ;)

avatar image eshonbel · May 08, 2014 at 06:00 PM 0
Share

Just wondering is there a way to do the same stuff, but is there a way to use the same script, but do it so i do not have to hold the button, just press it and it stays like that, and when I press it again it goes out of the ads. Sorry for bad grammar, I'm on my phone.

avatar image robertbu · May 08, 2014 at 06:44 PM 0
Share

Try this:

 #pragma strict
  
 var aim : boolean = false; 
 var cam : GameObject;
  
 private var rect = Rect(20,40,80,20);
  
 function OnGUI () { 
  
     var e = Event.current;
  
     if (e.type == EventType.$$anonymous$$ouseDown && rect.Contains(e.mousePosition)) {
        aim = true;
        cam.SetActive(!cam.activeSelf);
        Debug.Log("Finger Down");
     }
  
        GUI.Button (Rect (20,40,80,20), "ADS");
 }
avatar image eshonbel · May 09, 2014 at 06:56 AM 0
Share

Thanks so much dude, I will be featuring you in the credits of my game ;)

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

20 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 avatar image avatar image avatar image

Related Questions

Optimize for Windows 10 Mobile 0 Answers

Is the new UI system still not recommended for mobile? (Unity 5) 1 Answer

Gui Playing Audio Trouble 1 Answer

How can I view a gameObject through every other gameObject? 0 Answers

how to put the GUI into a gameobject so that when clicked, The GUI pops up, and heres a sample multiple Choice question using GUI, i need the code for it. 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