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!
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:
Replace the code you have by the one below
Increase the size of
keypadButtons
arrayFill the "basic" information : TargetCollider, Animator, AnimationClipName
In the
OnPress
box, click on the "+" button, drag & drop the object holding theIdentifyObjectsScript
script, and bind the correct functions to call (`AppendCode`,ClearCode
,CheckCode
)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() ; } } } } }
@Hellium This is exactly what I was asking, thank you kind sir!
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();
}
}
}