Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by vbs · Dec 06, 2017 at 02:57 AM · raycastvr

RayCasting Unity 2017.3.f10 Oculus Rift VR, not originating from center of viewport/camera

I have an issue trying to cast a ray outward directly from the center of the viewport or camera, (from the middle of the head, between the eyes) straight outward in order to hit whatever the player might be looking at.

I've tried 2 different ways, and both seem to produce the exact same result

I have tried both

Ray ray = cam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));

and

Ray ray = new Ray(m_Camera.position, m_Camera.forward);

as well as visualizing the rays with

Debug.DrawRay(ray.origin, ray.direction * m_DebugRayLength, Color.green, m_DebugRayDuration);

and

Debug.DrawRay(m_Camera.position, m_Camera.forward * m_DebugRayLength, Color.yellow, m_DebugRayDuration);

and for me the direction of the ray is good, but the origin is never the center of the camera / viewport on the Y axis. the Y axis seems to be 0, even though my camera is clearly not at 0 on the y axis. (rays shoot from the feet of my character in VR, using Oculus Rift. Unity 2017.3.f10

Here is my code in it's entirety, it's a bit of a mess, trying both options

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using VRStandardAssets.Utils;
 
 public class SiTeleport : MonoBehaviour
 {
 
     public VREyeRaycaster Raycaster;
     public float Radius = 0.5f;
     public Vector3 RadiusVector;
     public Transform rayHitIndicatorObject;
 
     private VRInteractiveItem oldPlatform;
     private Vector3 oldHitPoint;
     private CharacterController Controller;
     private bool triggerIsPressed;
 
     public Camera cam;
     public Transform m_Camera;
     [SerializeField] private LayerMask m_ExclusionLayers;           // Layers to exclude from the raycast.
     [SerializeField] private float m_RayLength = 500f;              // How far into the scene the ray is cast.
     [SerializeField] private bool m_ShowDebugRay;                   // Optionally show the debug ray.
     [SerializeField] private float m_DebugRayLength = 5f;           // Debug ray length.
     [SerializeField] private float m_DebugRayDuration = 1f;         // How long the Debug ray will remain visible.
 
 
     void Awake()
     {
         Controller = gameObject.GetComponent<CharacterController>();
 
         if (Controller == null)
             Debug.LogWarning("OVRPlayerController: No CharacterController attached.");
     }
 
     // Use this for initialization
     void Start()
     {
         triggerIsPressed = false;
         RadiusVector = new Vector3(Radius, 0, Radius);
     }
 
     // Update is called once per frame
     void Update()
     {
         //VRInteractiveItem platform = Raycaster.CurrentInteractible;
         //if (platform != null && platform != this.oldPlatform)
         //{
         //    Debug.Log(platform);
         //}
         //this.oldPlatform = platform;
 
         EyeRaycast();
     }
 
     private void EyeRaycast()
     {
         Ray ray = cam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
         // Create a ray that points forwards from the camera.
         //Ray ray = new Ray(m_Camera.position, m_Camera.forward);
         RaycastHit hit;
 
         // Show the debug ray if required
         if (m_ShowDebugRay)
         {
             Debug.DrawRay(ray.origin, ray.direction * m_DebugRayLength, Color.green, m_DebugRayDuration);
 
 
             //Vector3 cameraPosition = m_Camera.transform.position;
             //Debug.DrawRay(m_Camera.position, m_Camera.forward * m_DebugRayLength, Color.yellow, m_DebugRayDuration);
             //Debug.DrawRay(cameraPosition, m_Camera.forward * m_DebugRayLength, Color.yellow, m_DebugRayDuration);            
         }
 
         // Do the raycast forweards to see if we hit an interactive item
         if (Physics.Raycast(ray, out hit, m_RayLength, ~m_ExclusionLayers))
         {
             float triggerValue = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger);
             //Debug.Log(triggerValue);
 
             Vector3 currentHitPoint = hit.point;
             //if (currentHitPoint != oldHitPoint)
             //{
             //    //Debug.Log(hit.point);
             //}
             rayHitIndicatorObject.position = currentHitPoint;
 
             if (triggerValue > 0.5f)
             {
                 if (triggerIsPressed)
                     return;
 
                 triggerIsPressed = true;
 
                 // move to location looked at
                 transform.position = new Vector3(currentHitPoint.x, transform.position.y, currentHitPoint.z);
             }
             else
             {
                 triggerIsPressed = false;
             }
             
         }
     }       
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

171 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GvrReticlePointer instead RAYCASTHIT.point 0 Answers

Using Zed Camera Mesh Maker VR 0 Answers

raycast activation by trigger 0 Answers

Unity Daydream Preview - Raycast/camera alignment 1 Answer

Using Raycast and Layermask to teleport player in VR, but the Raycast teleport initiates through objects of a different layer 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges