Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raycasting from Controller

Discussion in 'VR' started by amit_barde, Aug 22, 2018.

  1. amit_barde

    amit_barde

    Joined:
    Aug 19, 2018
    Posts:
    1
    Hello!

    This is my first post here. I've been attempting, for the last 3 days, to achieve a Raycast from the right controller of the Dell Visor Windows Mixed Reality Headset and haven't been able to get anywhere! I've looked over here, the Microsoft forums, gone through the hand controller tutorial videos and documentation on Microsoft's website. I am completely lost and would really love some assistance trying to get this Raycast from the controller to work.
    I'm trying to use the controller as gun and would like to 'shoot' down flying objects. This is what I have for a Shoot script right now.

    void Shoot()
    {
    RaycastHit hit;

    if (Physics.Raycast(MixedReality_Cam.transform.position, MixedReality_Cam.transform.forward, out hit, range))
    {
    Debug.DrawRay(MixedReality_Cam.transform.position, MixedReality_Cam.transform.forward, Color.blue);
    Target target = hit.transform.GetComponent<Target>();
    if (target != null)
    {
    target.TakeDamage(damage);
    }

    Debug.Log(hit.transform.name);
    }

    }

    As you can see, for now I've used the Mixed Reality Camera that is attached to the Mixed Reality Camera Parent as point from which the Ray is sent out into the environment. This is obviously not ideal. I would be extremely thankful if someone could point me in the right direction.

    Thanks! :)
     
  2. JasonCostanza

    JasonCostanza

    Unity Technologies

    Joined:
    May 23, 2017
    Posts:
    404
    Hey @amit_barde

    sorry for the late-late reply. You can use the TryGet things outlined here:
    https://docs.unity3d.com/ScriptReference/XR.WSA.Input.InteractionSourcePose.html

    For something like what you're building, I would probably suggest looking into the pointer forward attribute, which shoots out perpendicular to the ring on the MR controllers. From there give the raycast you shoot the standard parameters of origin (grip pointer), direction (pointer forward), out target, and range. Pull that together you should have a behavior I think you're looking for.

    Let me know where that gets you, good luck