Click on object with Raycast screen - Unity [ENG]

  Рет қаралды 36,260

Krister Cederlund

Krister Cederlund

7 жыл бұрын

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
@eyeballjuice
@eyeballjuice 4 жыл бұрын
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!
@MarcioAOliveirabr
@MarcioAOliveirabr 5 жыл бұрын
Great tutorial, thank you.
@brettchiotasso705
@brettchiotasso705 5 жыл бұрын
Thank you this helped me.
@_Garm_
@_Garm_ 7 жыл бұрын
Great tutorial, keep em comming ! :D
@KristerCederlund
@KristerCederlund 7 жыл бұрын
Thank you :)
@GrupPasoai
@GrupPasoai 5 жыл бұрын
obyects, nice tutorial btw
@pavelbrazda3626
@pavelbrazda3626 4 жыл бұрын
Thank you very much
@jinsormae1640
@jinsormae1640 6 жыл бұрын
nice, I like this tutorial :D
@Tristannn-
@Tristannn- 3 жыл бұрын
IT WORKED, THANK YOU BRO
@homevideos.
@homevideos. 6 жыл бұрын
great video!
@CaptainBlackSea
@CaptainBlackSea 4 ай бұрын
thank you, you make me see my a code fail.
@otabekbaratov117
@otabekbaratov117 4 жыл бұрын
THANK YOU BRO !!!
@MemerManStan
@MemerManStan Жыл бұрын
Thanks bro
@cosmology2773
@cosmology2773 4 жыл бұрын
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!
@aboziad100
@aboziad100 3 жыл бұрын
what if i want to make panel popup when selecting touch the object in AR application. can you please suggest me thank you
@kadper
@kadper 2 жыл бұрын
Tack!
@pfp4ever
@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
@seemstwo8919
@seemstwo8919 4 жыл бұрын
i think you forgot to have it actually do something with different objects ;/
@luisera79
@luisera79 5 жыл бұрын
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
@Fluffyphish
@Fluffyphish 5 жыл бұрын
Make sure "current" is lowercase.
@magicquest5221
@magicquest5221 5 жыл бұрын
you have to use using UnityEngine.EventSystems;
@silverarrow7753
@silverarrow7753 5 жыл бұрын
I guess I've done everything like on video, but ZeroRefferenceExeption is still there. Even after adding Event System. Can someone help?
@noobmaximnoob
@noobmaximnoob 5 жыл бұрын
Tag your camera as 'Main Camera' in the inspector
@seddaouiyassine7814
@seddaouiyassine7814 5 жыл бұрын
@@noobmaximnoob LiveSafer
@itaysr9595
@itaysr9595 4 жыл бұрын
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
@macleodgordon
@macleodgordon 5 жыл бұрын
What has to be changed to make this work on a 2D gameobject, like a sprite?
@sammuelbonin2182
@sammuelbonin2182 5 жыл бұрын
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"); } } }
@barunbairagi1983
@barunbairagi1983 4 жыл бұрын
Hi @@sammuelbonin2182 , Thanks a lot for your code for 2D game Object. It really helped me a lot.
@sammuelbonin2182
@sammuelbonin2182 4 жыл бұрын
@@barunbairagi1983 no problem, happy to help
@mrarne2749
@mrarne2749 3 жыл бұрын
@@sammuelbonin2182 Hey what should i channge the source script and the SourceCall to? sorry for the noob question
@sammuelbonin2182
@sammuelbonin2182 3 жыл бұрын
@@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.
@franklambeth1148
@franklambeth1148 2 жыл бұрын
anyway to link animations to it
@GEORGE.M.M
@GEORGE.M.M 6 жыл бұрын
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.
@conceptfactory5604
@conceptfactory5604 6 жыл бұрын
Don't recognize if I click in UI objects
@adiv197
@adiv197 5 жыл бұрын
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.
@motionteam7274
@motionteam7274 6 жыл бұрын
it's not working in my Project
@KristerCederlund
@KristerCederlund 6 жыл бұрын
+Motion Team that's not good, maybe you missed a step or something.
@seddaouiyassine7814
@seddaouiyassine7814 5 жыл бұрын
Tag Your Camera as Main Camera
@thomastramper3637
@thomastramper3637 4 жыл бұрын
@@seddaouiyassine7814 thanks!
@SiegeMinion
@SiegeMinion 5 жыл бұрын
Not working.
@SiegeMinion
@SiegeMinion 5 жыл бұрын
Not working.
4 Ways to Find Your Mouse Click in World Space
22:30
Board To Bits Games
Рет қаралды 60 М.
Water powered timers hidden in public restrooms
13:12
Steve Mould
Рет қаралды 723 М.
I'm Excited To see If Kelly Can Meet This Challenge!
00:16
Mini Katana
Рет қаралды 24 МЛН
Finger Heart - Fancy Refill (Inside Out Animation)
00:30
FASH
Рет қаралды 28 МЛН
Викторина от МАМЫ 🆘 | WICSUR #shorts
00:58
Бискас
Рет қаралды 4,6 МЛН
Jumping off balcony pulls her tooth! 🫣🦷
01:00
Justin Flom
Рет қаралды 26 МЛН
Selecting Objects with Raycast - Unity Tutorial
9:31
Infallible Code
Рет қаралды 228 М.
Rotate towards object in 3D - Unity [ENG]
4:04
Krister Cederlund
Рет қаралды 62 М.
Unity Tutorial : Drag Gameobject with Mouse
3:45
Jayanam
Рет қаралды 142 М.
3 ways to do a Ground Check in Unity
15:58
Code Monkey
Рет қаралды 222 М.
OPENING a DOOR using a BUTTON
18:38
SpeedTutor
Рет қаралды 24 М.
Three.js tutorial: Highlight selected object
12:31
Coding for Superheroes
Рет қаралды 2,8 М.
Pinch and Scroll to Move and Zoom in Unity for Mobile Games
13:19