I just want to know how to make the object you are pointing at in the middle of the screen play an animation. Also if you can I kind of want to make a crosshair so you can aim better.
Do you mean something like in a first-person shooter, when you point the camera at something, it activates?
- Put a collider on the object you want to trigger
- Put a script on that object, let’s call it RaycastReceiver
- on a script on the main camera, use Physics.Raycast and Camera.main.ViewportPointToRay(0,0), and make sure you get the RaycastHit object from that.
- If hit.collider.GetComponent() != null, then you can call a function on RaycastReceiver to do anything, such as play your animation.
If you need help with any of these steps in particular, ask
so these are 3 different questions.
And this is how you should be breaking up your problem solving… into its smaller parts.
-
how do you get a reference to an object that is in the middle of the screen?
You can do a raycast with a ray that you get from Camera.ViewportPointToRay. Check this out:
Unity - Scripting API: Camera.ViewportPointToRay -
how to play an animation on an object?
That’s a fairly complex question as there are 2 animation systems in Unity, and we don’t know which you use. Check out unity’s animation tutorials for more information on specifics. -
how to make a crosshair?
Stick a graphic in the middle of screen…
Try googling ‘unity crosshair’…
Thank you so much!!
You surely meant 0.5f, 0.5f ![]()