How to make object in middle of screen play an animation

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?

  1. Put a collider on the object you want to trigger
  2. Put a script on that object, let’s call it RaycastReceiver
  3. 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.
  4. 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.

  1. 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

  2. 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.

  3. 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 :stuck_out_tongue: