crosshair interaction

I have a 3rd person camera for exterior shots, when the player goes indoors ( small room) the camera switches to first person camera with an aiming crosshair. question is: I want the player to interact with what ever game object is in the cross hair or trigger it’s animation. So if a book ( with animation on it of it opening) is in the crosshairs and I mouseclick, the book opens. I know how to do 90% of all this, I just need a script or something that recognizes the book is in the crosshairs and that my mouseclick animates just that game object. The crosshair is a sprite on the UI canvas. Do i need to make it a gameobject and child to the camera instead so it ACTUALLY DOES physically touch the book( instead of visually overlayed upon it as a UI element) thus triggering the animation with a OnTriggerEnter script? I guess that would work but seems clumsy as the objects that will be interacted with will be at various distances from the camera. I guess I could do both? A UI crosshair for visual referenece but an invisible game object trigger constrained to the camera…gameobject being quite long to get the depth but narorow so as not to trigger objects on either side of the book? MODERATORS…please post as I have not found anything here even close to addressing this issue

Do you have the AudioClip clip assigned to this script in the inspector, any errors? Because the AudioSource.Play() plays the one assigned to audiosource, I think.

1 Answer

1

Cast a Physics Ray from the camera with a short distance. If the ray hits anything of interest then you can consider it “touching”. Handle interaction logic accordingly.

http://docs.unity3d.com/Manual/CameraRays.html

http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html

I have it there and no errors... I'm just confused.