I will show you how to know if the player click on an object with the mouse by raycasting from the screen mouse position.
Пікірлер: 41
@eyeballjuice4 жыл бұрын
thank you so much mate. im inexperienced with unity and forgot how to do this. been searching for an easy explanation of how to raycast on objects and detect their name for ages. thank you!
@MarcioAOliveirabr5 жыл бұрын
Great tutorial, thank you.
@brettchiotasso7055 жыл бұрын
Thank you this helped me.
@_Garm_7 жыл бұрын
Great tutorial, keep em comming ! :D
@KristerCederlund7 жыл бұрын
Thank you :)
@GrupPasoai5 жыл бұрын
obyects, nice tutorial btw
@pavelbrazda36264 жыл бұрын
Thank you very much
@jinsormae16406 жыл бұрын
nice, I like this tutorial :D
@Tristannn-3 жыл бұрын
IT WORKED, THANK YOU BRO
@homevideos.6 жыл бұрын
great video!
@CaptainBlackSea4 ай бұрын
thank you, you make me see my a code fail.
@otabekbaratov1174 жыл бұрын
THANK YOU BRO !!!
@MemerManStan Жыл бұрын
Thanks bro
@cosmology27734 жыл бұрын
I'm a bit confused by your if statement. It's like it will only accept one specific type of raycast in order to do that if statement, so it requires that the object be 100 away, but it's not doing that, so I'm confused. Also, in the context of an if statement, what is "out hit" doing? It's just checking to see if there's something it's hitting? Thanks!
@aboziad1003 жыл бұрын
what if i want to make panel popup when selecting touch the object in AR application. can you please suggest me thank you
@kadper2 жыл бұрын
Tack!
@pfp4ever Жыл бұрын
:( working on something simple like this just to figure out i have a 2d collider that does not get detected in the same way
@seemstwo89194 жыл бұрын
i think you forgot to have it actually do something with different objects ;/
@luisera795 жыл бұрын
I get this error Assets aycast.cs(12,67): error CS0117: 'UnityEventQueueSystem' does not contain a definition for 'Current' and I can't figure it out, help please
@Fluffyphish5 жыл бұрын
Make sure "current" is lowercase.
@magicquest52215 жыл бұрын
you have to use using UnityEngine.EventSystems;
@silverarrow77535 жыл бұрын
I guess I've done everything like on video, but ZeroRefferenceExeption is still there. Even after adding Event System. Can someone help?
@noobmaximnoob5 жыл бұрын
Tag your camera as 'Main Camera' in the inspector
@seddaouiyassine78145 жыл бұрын
@@noobmaximnoob LiveSafer
@itaysr95954 жыл бұрын
I've been on this for 3 hours now, I have practically gone insane. I see this comment, notice how my camera isn't tagged and tag it, and boom. it works. im completely raging as this is so stupid of a problem
@macleodgordon5 жыл бұрын
What has to be changed to make this work on a 2D gameobject, like a sprite?
@sammuelbonin21825 жыл бұрын
here's my 2d mouse click script, im in the opposite boat and needed 3d, void Update () { MouseInput(); } // void MouseInput() { if (Input.GetMouseButtonDown(0)) { Debug.Log("left mb hit"); RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (hit.collider.tag == null) { Debug.Log("null hit"); } else if (hit.collider.tag == "source") // change this to a tag you use { Debug.Log(hit.collider.gameObject); hit.collider.gameObject.GetComponent().SourceCall(); } else { Debug.Log("nil"); } } }
@barunbairagi19834 жыл бұрын
Hi @@sammuelbonin2182 , Thanks a lot for your code for 2D game Object. It really helped me a lot.
@sammuelbonin21824 жыл бұрын
@@barunbairagi1983 no problem, happy to help
@mrarne27493 жыл бұрын
@@sammuelbonin2182 Hey what should i channge the source script and the SourceCall to? sorry for the noob question
@sammuelbonin21823 жыл бұрын
@@mrarne2749 i should have added a comment explaining that line like i did for the tag, 'SourceScript' is supposed to the name of the script on the object clicked, 'SourceCall' is the function you are calling in the script, so, say you click on a button starts a timer, you would want to do something like else if (hit.collider.tag == "Button") // change this to a tag you use { Debug.Log(hit.collider.gameObject); hit.collider.gameObject.GetComponent().StartTimer(); } since i've shared this snippet, I have found better ways to go about this, depending on specifics of the situation.
@franklambeth11482 жыл бұрын
anyway to link animations to it
@GEORGE.M.M6 жыл бұрын
Hey Great tutorial!! quick question, how would I select and object using the raycast I'm operating using the xbox controller, instead of a mouse? RaycastHit hit; float theDistance; //Debug Raycast in the Editor - SO WE CAN SEE IT! - MAKES LIFE EASIER! //Vector3 forward = transform.TransformDirection(Vector3.forward) * 10; Vector3 back = new Vector3(Input.GetAxis("XBOX.1.RightStick.X") * Time.deltaTime * 10, Input.GetAxis("XBOX.1.LT") * Time.deltaTime * 10, Input.GetAxis("XBOX.1.RightStick.Y") * Time.deltaTime * 10); Debug.DrawRay(transform.position, back, Color.green); if (Physics.Raycast(transform.position, (back), out hit)) { if (hit.collider.gameObject.tag == "tile") { theDistance = hit.distance; print(theDistance + " " + hit.collider.gameObject.name); if (Input.GetButton("XBOX.1.RightStick.Press")) { Cube.Selected = this.gameObject; } } } I created a separate class called raycastFoward, and the item I want to select is a class called Cube. I end up selecting the drawn ray, Im trying to select a game of object from another class instead, is this the wrong way to pass the argument, plus my script is attached to a game object child of my main camera.
@conceptfactory56046 жыл бұрын
Don't recognize if I click in UI objects
@adiv1975 жыл бұрын
UI objects aren't on the world space, they're overlays, so either add a button script or change the canvas type to world space.
@motionteam72746 жыл бұрын
it's not working in my Project
@KristerCederlund6 жыл бұрын
+Motion Team that's not good, maybe you missed a step or something.