• 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 Doneyes · Aug 31, 2012 at 09:18 PM · raycastbuttonswitche

How can I use raycast to activate a button?

I need to use raycast to see if I am facing an object and if I am within a few feet and and press E, //something happens.

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 Anusha · Sep 03, 2012 at 07:40 AM 0
Share

please accept the answer if this solution worked...

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by calebebrim · Jul 17, 2016 at 03:05 PM

I'm usin the following class to handle hold Action.

 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 using System.Collections;
 using System;
 
 
 
 public class ButtonHoldBehaviour : MonoBehaviour{
 
     public Action onHold;
     public bool hold = false;
     void Start () {
         onHold = () => {
             print ("No Button Behaviour for: " + gameObject.name);
         };
     }
     
     void Update () {
         hold = false;
         if(Input.touches.Length==1){
             if (Input.touches[0].deltaTime>0 ) {
                 hold = hover (Input.touches [0].deltaPosition, transform.gameObject);
             }
 
         }
 
         if (Input.GetMouseButton (0)) {
             hold = hover (Input.mousePosition, transform.gameObject);
         } 
 
         if (hold)
             onHold ();
     }
 
     private bool hover(Vector3 pos, GameObject desired ){
         RectTransform r = desired.GetComponent<RectTransform> ();
         return RectTransformUtility.RectangleContainsScreenPoint (r, pos);
     }
 }

It might be usefull @Doneyes. It worked for 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 Aggressor · Sep 02, 2012 at 03:35 AM

To further clarify and help with the above answer, I assume you are using an FPS and the raycast is from the players POV yes?

If so you can add a tag, and you can add a distance variable to the ray cast (in the above answer the 100.0F is a float specifying the distance range, so sub that for what you want the range to be).

You will need to tag the object with a layer (e.g. "Button1") so make sure its on the object.

The logic of the above code is: Cast a ray out 100F, from the forward direction (he uses up, but you could also use the Vector3.fwd instead). If the ray cast hits something, check its tag. If that tag is the button poll (i.e. keep checking) if the user presses the button (in this case he used E).

Make sense?

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
2

Answer by Anusha · Sep 01, 2012 at 05:56 AM

its just rough code.. havent tested it... tag the object u want to face - "Obj1"

`update() {

   RaycastHit hit;
        if (Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0F))
          {
            if(hit.transform.tag == "Obj1)
            {   // ur in 100 meters of the object
                  if (Input.GetKeyDown(KeyCode.E))
                    {  //u hit  E do something
                    }
           }
         }
 } `

  




Comment
Add comment · Show 8 · 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 Doneyes · Sep 02, 2012 at 01:06 AM 0
Share

is this #C?

avatar image Aggressor · Sep 02, 2012 at 01:22 AM 0
Share

Yes, in JS you dont need to specify a float next the value (as you do with C# where he has 100.0F)

avatar image Bunny83 · Sep 02, 2012 at 01:34 AM 0
Share

This works of course, but unless you want to visualize that the user can push the button, it's better to reverse the two things (inputcheck and raycast)

 // C#
 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
 {
     if (Physics.Raycast(transform.position, transform.forward, out hit, 2.0F))
     {
         if(hit.transform.tag == "Obj1")
         {
             // Press the button :D
         }
     }
 }
avatar image Doneyes · Sep 02, 2012 at 02:25 AM 0
Share

How would this work for javascript?

avatar image Bunny83 · Sep 02, 2012 at 01:05 PM 0
Share

Almost the same. There are only $$anonymous$$imal differences: variable declarations; function declaration; the "out" keyword isn't used in UnityScirpt:

 // UnityScript (Unity's Javascript)
 function Update()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
     {
         var hit : RaycastHit;
         if (Physics.Raycast(transform.position, transform.forward, hit, 2.0F))
         {
             if(hit.transform.tag == "Obj1")
             {
                 // Press the button :D
             }
         }
     }
 } `
Show more comments

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

RayCast Activating A Button 0 Answers

What am i doing wrong? I want to make a raycast acces panel with lights 2 Answers

Toggle Scripts On & Off 1 Answer

Issue with clicking with a crosshair while using Oculus Rift 1 Answer

How to develop a script that substitute a gameobject position with my character? 0 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