Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
nigrin's avatar
nigrin
Explorer
13 years ago

[TIP] Detecting where the player looks at

Hi guys,

we came across this for our vrjam game "Sorcerer's Wrath", where the player fights things by just looking at them.

Here is a quick blog post how we did this, I have not seen this specific approach in the wild and thought it might be worth sharing. Happy to discuss pro's and con's.

18 Replies

Replies have been turned off for this discussion
  • ssshake's avatar
    ssshake
    Honored Guest
    what did you write lunar flight in btw? I bought it, but I hadn't gotten around to playing it yet.
  • "ssshake" wrote:
    what did you write lunar flight in btw? I bought it, but I hadn't gotten around to playing it yet.


    C#
  • Hi, i've written a similar script, add some collider on the object I want to look at. But now, maybe a really stupid question: Until now, all the scripts i've written had to be add on an object. So my question is, where have I to put this script to make it works ?

    Hi Nigrin,

    Thanks for sharing that. I tackled the same problem and decided to use the Physics.Raycast() approach. Your approach may be much faster I don't know, I've heard raycasting is slow, but in my situation it didn't matter and I've not seen any speed impact.

    I'm pretty new to Unity so my approach is probably poor but I thought I'd share it (written in C#) :-

    public class PlayerLookingAt : MonoBehaviour {

    Transform cameraTransform = null;

    void Awake() {
    cameraTransform = GameObject.FindWithTag("MainCamera").transform;
    }

    void Update() {
    float length = 10.0f;
    RaycastHit hit;
    Vector3 rayDirection = cameraTransform.TransformDirection(Vector3.forward);
    Vector3 rayStart = cameraTransform.position + rayDirection; // Start the ray away from the player to avoid hitting itself
    Debug.DrawRay(rayStart, rayDirection * length, Color.green);
    if (Physics.Raycast(rayStart, rayDirection, out hit, length)) {
    if (hit.collider.tag == "Tower") {
    // Do stuff
    }
    }
    }
    }


    That's the guts of the code. I've tagged the OVR camera controller as 'MainCamera' and put a collider with isTrigger ticked on the objects I want to know if the player is looking at.

    I hope that helps someone,
    Cheers,
    Mark


    Thanks for your future answers.
    Cheers,
    Joffrey.
  • vajra3d's avatar
    vajra3d
    Honored Guest
    If your code is referencing the camera itself (as it is w/ the example code) then you can just attach it to virtually any gameObject you want and it should work for you.

    If you are going to be casting to only one object then you might want to attach it to the target object itself otherwise if you are going to cast to multiple objects then you might consider attaching it to the playerController for convenience sake.

    You can also just create an empty gameObject, give it a relevant name, and attach it to that. Good luck!
  • I just want to be able to display the object's name that I'm looking at. Thank you for your answer but it still doesn't work. Here is my code:
    using UnityEngine;
    using System.Collections;

    public class PlayerLookAtDisplay : MonoBehaviour {

    Transform cameraTransform = null;

    void Awake() {
    cameraTransform = GameObject.FindWithTag("MainCamera").transform;
    }

    void Update() {
    float length = 10.0f;
    RaycastHit hit;
    Vector3 rayDirection = cameraTransform.TransformDirection(Vector3.forward);
    Vector3 rayStart = cameraTransform.position + rayDirection;
    Debug.DrawRay(rayStart, rayDirection * length, Color.blue);
    if (Physics.Raycast(rayStart, rayDirection, out hit, length)) {
    GUI.Label(new Rect(Screen.width / 5 * 1, 400, 200, 50), "TOUCH"); //VR
    GUI.Label(new Rect(Screen.width / 5 * 3, 400, 200, 50), "TOUCH");
    }
    }
    }


    It's quite the same but i've tried to attach it to a simple cube, it doesn't work. I have taged my OVRCameraController to MainCamera as explain, then I attached my script to the cube. When I'm looking at my cube, nothing happens. I've tried to attach the script to my camera, to the cube and my camera, still nothing. I really don't know what I'm doing wrong >< I really need to display the name of the object, and I don't know a better way to do that.
  • vajra3d's avatar
    vajra3d
    Honored Guest
    Does the cube have a collider attached to it? In order for the ray to detect an object in the scene it must have a physics collider component (box, capsule, mesh...) attached to it. That should do the trick.

    Also, if you want it to display the name of the object it hits then add something like this to your code:

    print (hit.collider.name);

    or

    print (hit.collider.gameObject.name);

    also, if using tags:

    print (hit.collider.tag);

    Hope that helps!
  • Thank you, i've deleted my collider , don't know why, now it works !

    -Joffrey
  • Havauk's avatar
    Havauk
    Honored Guest
    Hi all, I was looking for some raycast tutorials for Oculus and Unity and I found this page with some really useful stuff ! I was able to use the code from marky in my scene (I attached the script to the RightEyeAnchor of OVR CameraRig) and it works...well, almost works.

    In my scene, there are some planes that plays a video (with a Movie Texture) and I want my ray to detect thoses "screens", so I added the tag "Screen" to the planes and replaced "Tower" by "Screen" in marky's code and to see if that works, I added
    Debug.DrawRay(rayStart, rayDirection * length, Color.red);
    instead of "Do stuff".

    So in my scene, the ray is supposed to turn red when it hits a screen, but it doesnt work. It works on a plane with a simple image on it, and IsTrigger is not even checked.

    I know the last answer in this post is 1 year old, but if anyone could help me, I would truly be grateful :)

    Thanks !