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 /
  • Help Room /
avatar image
0
Question by smarjanovicng · Nov 29, 2018 at 09:30 PM · raycastraycastingraycasthit

Can i use raycast for something like a keypad?

In my game i got a keypad next to a door and i got a raycast on my Player camera, how would i tell the game that the player hit a button? I have an idea how to tell the game if a code for the door is right but i dont know how to tell the game that the raycast hit the particular object. Prob something like this ..
public string input;
private string Code = "1234";

void checkRaycast(){
if(raycast hit the button1){
input = input + 1;
}
if(raycast hit the button2){
input = input + 2;
}
if....
}
..........................................
void CheckTheCode(){
if(input == Code){
UnlockDoor();
}

}

The checkRaycast method is my problem, thanks good people of the internet!

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

2 Replies

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

Answer by Hellium · Nov 30, 2018 at 08:02 AM

But is this considerd ok programming?

It's "ok" since it works, but it could be : simplified, optimized and more flexible:

  1. Replace the code you have by the one below

  2. Increase the size of keypadButtons array

  3. Fill the "basic" information : TargetCollider, Animator, AnimationClipName

  4. In the OnPress box, click on the "+" button, drag & drop the object holding the IdentifyObjectsScript script, and bind the correct functions to call (`AppendCode`, ClearCode, CheckCode)

  5. Try and enjoy


      [System.Serializable]
         public struct KeypadButton
         {
             public Collider TargetCollider;
             public Animator Animator;
             public string AnimationClipName;
             public UnityEngine.Events.UnityEvent OnPress;
         }
         
         public class IdentifyObjectsScript : MonoBehaviour
         {
             [Header("Camera")]
             private RaycastHit hit;
             public float range = 10;
             public Camera cam;
             [Header("Keypad")]
             private string rightCode = "3567";
             public string input= "";
             public AudioSource buttonPress;
             public AudioSource inputIsCorrect;
             public KeypadButton[] keypadButtons;
             
             void Update ()
             {
                 if (Input.GetKeyDown(KeyCode.F))
                 {
                     RaycastKeypad();
                 }
             }
         
             public void AppendCode( string code )
             {
                 input = input + code;
             }
         
             public void ClearCode()
             {
                 input = string.Empty;
             }
             
             public void CheckCode()
             {
                 if( string.Equals( rightCode, input ) )
                 {
                     Debug.Log("LOZINKA VALJA");
                     inputIsCorrect.Play();
                 }
             }
         
             void RaycastKeypad()
             {
                 if( Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) )
                 {
                     for (int keypadIndex = 0; keypadIndex < keypadButtons.Length; keypadIndex++)
                     {
                         KeypadButton keypadButton = keypadButtons[keypadIndex]
                         if( hit.collider == keypadButton.TargetCollider )
                         {
                             if( keypadButton.Animator != null )
                                 keypadButton.Animator.Play( keypadButton.AnimationClipName ) ;
                             if( keypadButton.OnPress != null )
                                 keypadButton.OnPress.Invoke() ;
                         }
                     }
                 }
             }
         }
    
    
    
    
    
    
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 smarjanovicng · Nov 30, 2018 at 12:19 PM 0
Share

@Hellium This is exactly what I was asking, thank you kind sir!

avatar image
0

Answer by smarjanovicng · Nov 30, 2018 at 05:22 AM

I found the soulution and it works! But is this considerd ok programming?

 public class IdentifyObjectsScript : MonoBehaviour {
 [Header("Camera")]
 private RaycastHit hit;
 public float range = 10;
 public Camera cam;

 [Header("Keypad")]
 private string rightCode = "3567";
 public string input="";
 public AudioSource buttonPress;
 public AudioSource inputIsCorrect;
 public Animator btn1AC;
 public Animator btn2AC;
 public Animator btn3AC;
 public Animator btn4AC;
 public Animator btn5AC;
 public Animator btn6AC;
 public Animator btn7AC;
 public Animator btn8AC;
 public Animator btn9AC;
 public Animator btn0AC;
 public Animator btnEAC;
 public Animator btnDAC;

 void Start () { 
 }

 void Update () {

     if (Input.GetKeyDown(KeyCode.F))
     {
         RaycastKeypad();
     }
 }

 #region RayCast Keypad
 void RaycastKeypad()
 {
     if( Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) && hit.collider.name == "button1")
     {
         btn1AC.Play("btn1");
         buttonPress.Play();
         input = input + "1";
     }
     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) && hit.collider.name == "button2")
     {
         btn2AC.Play("btn2");
         buttonPress.Play();
         input = input + "2";
     }
     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) && hit.collider.name == "button3")
     {
         btn3AC.Play("btn3");
         buttonPress.Play();
         input = input + "3";
     }
     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) && hit.collider.name == "button4")
     {
         btn4AC.Play("btn4");
         buttonPress.Play();
         input = input + "4";
     }
     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) && hit.collider.name == "button5")
     {
         btn5AC.Play("btn5");
         buttonPress.Play();
         input = input + "5";
     }
     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) && hit.collider.name == "button6")
     {
         btn6AC.Play("btn6");
         buttonPress.Play();
         input = input + "6";
     }
     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) && hit.collider.name == "button7")
     {
         btn7AC.Play("btn7");
         buttonPress.Play();
         input = input + "7";
     }
     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) && hit.collider.name == "button8")
     {
         btn8AC.Play("btn8");
         buttonPress.Play();
         input = input + "8";
     }
     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) && hit.collider.name == "button9")
     {
         btn9AC.Play("btn9");
         buttonPress.Play();
         input = input + "9";
     }
     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) && hit.collider.name == "ENTER")
     {
         btnEAC.Play("enter");
         buttonPress.Play();
         checkTheCode();
     }
     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) && hit.collider.name == "DELETE")
     {
         btnDAC.Play("delete");
         buttonPress.Play();
         input = "";
     }

 }
 #endregion

 void checkTheCode()
 {
     if(rightCode == input)
     {
         Debug.Log("LOZINKA VALJA");
         inputIsCorrect.Play();
     }
 }

}

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

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

Related Questions

Why isn't my ray working? 1 Answer

Need help changing material back when raycast no longer hitting object. 1 Answer

RayCast appears to bug out for no apparent reason 1 Answer

Raycast doesn't stay in one place 0 Answers

I'm Trying to get a 3D Model to follow a Raycast 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