Search Unity

VR Velocity Problem.

Discussion in 'Editor & General Support' started by chathaway102, Jun 21, 2019.

  1. chathaway102

    chathaway102

    Joined:
    Jun 20, 2019
    Posts:
    13
    Hello everyone. I recently started development on a VR climbing game. I got my inspiration from the game Sweet Escape VR. So anyways, I have all the controls down, and just recently finish with the velocity. (So you can fling yourself off objects and fly through the air to try and catch another one) This happens when you swing yourself in a direction and release the trigger. However, this is very constrained. You only fly through the air when you release at a perfect frame. For example, if the player swings and doesn't let go at the perfect frame, he either goes nowhere or goes down. I was wondering if there was a way in the scripts (posted below) to make some sort of grace period between swinging and letting go. For example in the situation where he doesn't let go fast enough, he would still fling forward as if he did. I have two scripts, Pull and Manager, where Pull is applied to the controllers and Manager is applied to the [CameraRig]. The manager script actually handles the code of climbing, and the pull script handles the boolean(s).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Manager : MonoBehaviour {
    6.  
    7.     public Rigidbody Body;
    8.  
    9.     public Pull left;
    10.  
    11.     public Pull right;
    12.  
    13.     void Start()
    14.     {
    15.         Body.constraints = RigidbodyConstraints.FreezePositionY;
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         var ldevice = SteamVR_Controller.Input((int)left.controller.index);
    21.         var rdevice = SteamVR_Controller.Input((int)right.controller.index);
    22.  
    23.         bool isGripped = left.canGrip || right.canGrip;
    24.  
    25.         if (isGripped)
    26.         {
    27.  
    28.             if (left.canGrip && ldevice.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
    29.             {
    30.                 Body.constraints = RigidbodyConstraints.None;
    31.                 Body.useGravity = false;
    32.                 Body.isKinematic = true;
    33.                 Body.transform.position += (left.prevPos - left.transform.localPosition);
    34.             }
    35.             else if (left.canGrip && ldevice.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
    36.             {
    37.                 Body.constraints = RigidbodyConstraints.None;
    38.                 Body.useGravity = true;
    39.                 Body.isKinematic = false;
    40.                 Body.velocity = (left.prevPos - left.transform.localPosition) / Time.deltaTime;
    41.             }
    42.  
    43.             if (right.canGrip && rdevice.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
    44.             {
    45.                 Body.constraints = RigidbodyConstraints.None;
    46.                 Body.useGravity = false;
    47.                 Body.isKinematic = true;
    48.                 Body.transform.position += (right.prevPos - right.transform.localPosition);
    49.             }
    50.             else if (right.canGrip && rdevice.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
    51.             {
    52.                 Body.constraints = RigidbodyConstraints.None;
    53.                 Body.useGravity = true;
    54.                 Body.isKinematic = false;
    55.                 Body.velocity = (right.prevPos - right.transform.localPosition) / Time.deltaTime;
    56.             }
    57.         }
    58.         else
    59.         {
    60.             Body.useGravity = true;
    61.             Body.isKinematic = false;
    62.         }
    63.  
    64.         left.prevPos = left.transform.localPosition;
    65.         right.prevPos = right.transform.localPosition;
    66.     }
    67. }
    68.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Valve.VR;
    5.  
    6. public class Pull : MonoBehaviour {
    7.  
    8.     public SteamVR_TrackedObject controller;
    9.  
    10.     [HideInInspector]
    11.     public bool canGrip;
    12.  
    13.     [HideInInspector]
    14.     public Vector3 prevPos;
    15.  
    16.     void Start () {
    17.         prevPos = controller.transform.localPosition;
    18.     }
    19.  
    20.     void OnTriggerEnter()
    21.     {
    22.         canGrip = true;
    23.     }
    24.  
    25.     void OnTriggerExit()
    26.     {
    27.         canGrip = false;
    28.     }
    29. }
    30.  
    Thank you so much for your help.