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
0
Question by svettgunnar · Sep 19, 2011 at 08:23 PM · androidraycastcolliderbutton

Android Button With Raycast

I just wonder if I can get a little help here, I need a few directions on how I will start with making buttons on an android platform with raycast - this is the script I got but how the hell will I make it work? do I just add raycast collider to the 3d text and add this script and it will work or how do I do?

 var particle : GameObject;  
 function Update () {  
        if (Input.GetButtonDown ("Fire1")) {  
         // Construct a ray from the current mouse coordinates  
         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);  
         if (Physics.Raycast (ray)) {  
             // Create a particle if hit  
             Instantiate (particle, transform.position, transform.rotation);  
             Application.LoadLevel(0);  
           
         }  
     }  
 }  
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

3 Replies

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

Answer by KureFlint · Sep 22, 2011 at 01:08 AM

If your making buttons for a mobile platform don't use raycasts. Use GUI. Or, and Highly recomend it, use Rect.Contains as a means of reaplacing GUI.

Here's an example of how it would look.

 //variables which construct the Rectangle
 var Rect = Rect(Screen.width,Screen.height,Screen.width,Screen.height);
 var HorizontalPositionx : float;
 var VerticalPositiony : float;
 var Width : float;
 var Height : float;
 
 var custom : GUIStyle;
 
 function Update(){
 Rect.y = Screen.height/VerticalPositiony;
 Rect.x = Screen.width/HorizontalPositionx;
 Rect.height = Screen.height/Height;
 Rect.width = Screen.width/Width;
 if (Rect.Contains(Input.mousePosition)){
 if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){
  // Create a particle if hit  
             Instantiate (particle, transform.position, transform.rotation);  
             Application.LoadLevel(0);  
     }
 }
 
 //you can edit this to show whatever your button is later but for now this just shows where your rect is
 function OnGUI(){
 GUI.Box(Rect, "Button Rect", custom);
 }


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 KureFlint · Sep 22, 2011 at 11:03 PM 0
Share

That just means I forgot to put a semicolon in there somewhere and that i forgot a bracket. I typed it up and the spot just to show you how it could be done so you could write your own, I didn't actually test it. Just put an extra bracket after Application.LoadLevel(0); and find where I forgot the semicolon. Also, add var particle : GameObject in the beginning if you don't already have it. Then it'll work.

Good Luck.

avatar image
0

Answer by 1337GameDev · Nov 07, 2011 at 09:59 PM

When programming for mobile devices, i am told that using unity OnGui is very processor intensive (as it has calls every frame) but to use a raycast or rectangle.contains instead.

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 ICEF1SCH · Aug 13, 2012 at 09:13 AM

Here's what I've got : (It is in C# but I think it should be easy to convert!)

  void Update() {

if (Input.GetButtonDown ("Melee")) { Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if (hit.transform == transform){ Application.LoadLevel(0); } } }

}

You need to add a RaycastHit to know that the button was hit!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

AndoidTouchControllJavaRaycast 0 Answers

NGUI UIButton Event question 1 Answer

Button Focus Prevents Touch Raycast (Android C#) 1 Answer

Detect only UI button click 3 Answers

I am making android fps and my raycast shooting button don't work. What I should do? 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