how to touch off a event when the centre of camrea look at a gameobject?
I think that best way to do taht is to use raycasting.
Raycast script reference: http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html
Use ScreenToWorldPoint function to get the center of the camera. Your argument for ScreenToWorldPoint should be Vector3(Screen.width/2, Screen.heigh/2, camera.nearClipPlane).
ScreenToWorldPoint script reference: http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenToWorldPoint.html
Example: (Attach it to an object that has a camera component)
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour
{
void Update()
{
Vector3 CameraCenter = camera.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.heigh/2, camera.nearClipPlane));
if (Physics.Raycast(CameraCente, transform.forward, 100))
Debug.Log("Ou yeah!");
}
}
p.s.
I didn't test this code ;)
As an alternative to the (Admittedly better) method presented above, you could just use the mouse.
You would use Cursor.lockState = CursorLockMode.Locked
to lock the cursor at the center of the screen and Cursor.visible = false
to hide it. Then use any of the OnMouse
… commands in MonoBehavior to make the object interactive. I guess this method would only be useful in certain circumstances (like the player picking up and carrying rigidbodies), but still worth a mention.