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 5facts · Oct 03, 2016 at 11:01 PM · raycastvrlayermaskteleporting

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

Hey there,

I've been trying my luck at VR game developing and have a few ideas in mind for which I'm currently devising a phone hardware stresstest to see what phones really are made of and what they can handle.

I own a thirdparty barebones cardboard contraption without the magnet button, so my main (and ONLY) method for user input is a tap on the screen. This can be a limiting factor, since I intend to be able to move around my demo level via teleportation and shoot a prefab cannonball projectile at enemies. With only one input method, I'm in a bit of a pickle here. My plan of action was to have the players touch input be interpreted either as a teleportation command if the player is looking at the floor defined as a teleportation layer for raycasting or as a fire projectile command, if the player is not looking at the floor, or is looking at an object in another layer.

My puny attempt so far works pretty well on the surface (except for Input.touchCount == 1 being executed every frame and things going wonky because of it but I'll try fix that myself) but what I've found is that the raycast will ignore objects like walls or scenery in another layer and teleport you through them, even though GazePoint() should be returning null because the raycast doesn't hit the defined teleportLayer. Yet, if your gaze point in VR space happens to hit the floor behind the object, it will initiate the teleport when it should fire a projectile instead, so every head tilt below flat horizontal will initiate a teleport, even when when standing square infront of another object.

I've tried to implement a fix for this for days so I wouldn't have to bother anyone with what I'm fearing might be a trivial matter of me not understanding the basics of raycasting but I've given up and I'd be very thankful for a nudge in the right direction!

 public class TeleportTo : MonoBehaviour
 {
     private RaycastHit lastRaycastHit;
     public LayerMask teleportLayer;
     GameObject prefab;
 
     void Start()
     {
         Cursor.visible = false;
         prefab = Resources.Load("cannonball") as GameObject;
     }
 
     private GameObject GazePoint()
     {
         Vector3 origin = transform.position;
         Vector3 direction = Camera.main.transform.forward;
         float range = 200;
         if (Physics.Raycast(origin, direction, out lastRaycastHit, range, teleportLayer.value))
             return lastRaycastHit.collider.gameObject;
         
         else 
             return null;
     }
 
     private void TeleportToGaze()
     {   
         transform.position = lastRaycastHit.point + lastRaycastHit.normal * 2;
     }
 
     void Update()
     {
         if (Input.touchCount == 1 && GazePoint () == null) 
         {     // This is basically fire projectile, so it can be ignored
        //     GameObject cannonball = Instantiate(prefab) as GameObject;
        //     cannonball.transform.position = transform.position + Camera.main.transform.forward * 3;
        //     Rigidbody rb = cannonball.GetComponent<Rigidbody>();
        //     rb.velocity = Camera.main.transform.forward * 500;
         }
 
         if (Input.touchCount == 1 && GazePoint () != null)
         {
             TeleportToGaze();
         }
 
     }
 
 }





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

107 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

Related Questions

Raycasting not working for specific object in VR 0 Answers

Block some raycasts from world space canvas and ignore others? 0 Answers

How to make a laser pointer with WebXR 0 Answers

Activate Button only by looking at it? 0 Answers

How do I make my raycast look up and down? 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