Search Unity

Should I use a time delay or raycast to detect if my VR player is standing on a platform?

Discussion in 'Scripting' started by CloudyVR, Apr 29, 2017.

  1. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    I have a level I built with SteamVR and my HTC Vive where a player can teleport and use roomscale in an environment with horizontally moving platforms.

    My problem is that when I use OnCollisionEnter accompanied by OnCollisionExit I get twitchy behavior because the feet of the VR player is in constant flux, meaning that they're not continuously being detected as being planted on the platform, thus Enter and Exit are being called frequently making my player slide off and kinda bounce all over the place.

    I tried calling OnCollisionEnter from the moving platform's script to set the player as one of it's children, and then I called OnCollisionEnter from the floor object's script that un-parents the player whenever he steps off the platform and onto the floor.

    That actually worked well but was causing other object to disassociate themselves from they're parent once they touched the floor;

    So I have given up on trying to get around this issue.

    How should I handle twitchy feet?

    I was thinking of either using a raycast in OnCollisionExit to determine if the player was still above the platform before setting the parent to null (or vice versa and check if the platform is still under the player...).

    Or possibly using a timer to delay the call to un-parent by a few hundred milliseconds and just reset the timer if it sees the feet touch the platform again?

    I was also wondering if maybe it would be simpler to completely stop using OnCollision events and instead just use raycasting in the player's Update function to always check if he/she is over a platform?

    Here's the code that causes the player to slide around:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class platformStay : MonoBehaviour {
    7.  
    8.     private IEnumerator coroutine;
    9.  
    10.     void OnCollisionEnter(Collision col){
    11.         if (col.transform.parent == null) {
    12.             col.transform.parent = transform;
    13.         }
    14.     }
    15.  
    16.     void OnCollisionExit(Collision col){
    17.         if (col.transform.parent != null) {
    18.             Vector3 newEmptyPos = col.transform.position; //the position (x, z) for the new empty
    19.             col.transform.parent = null;
    20.             col.transform.localPosition = newEmptyPos;
    21.         }
    22.     }
    23.  
    24. }
    25.  
    26.  
    Thanks
     
    Last edited: Apr 29, 2017
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Why not both?

    They both sound like reasonable solutions, just try to implement them and see how that works.