x


How can I check when the centre of camera is looking at a gameobject?

how to touch off a event when the centre of camrea look at a gameobject?

more ▼

asked Oct 11 '10 at 02:01 AM

Showken gravatar image

Showken
186 35 45 55

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 ;)

more ▼

answered Oct 11 '10 at 02:25 AM

IJM gravatar image

IJM
1.4k 2 5 19

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2987
x2075
x1525

asked: Oct 11 '10 at 02:01 AM

Seen: 1062 times

Last Updated: Oct 11 '10 at 03:07 AM