Created
August 1, 2019 20:18
-
-
Save Sarthakg91/878fd379038b2429ae868b67008b854a to your computer and use it in GitHub Desktop.
Detecting controller collisions in Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Valve.VR; | |
public class ControllerCollisonDetector : MonoBehaviour | |
{ | |
private bool inCollision = false; | |
private string colliderName = ""; | |
public SteamVR_Action_Vibration hapticAction; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
hapticAction = SteamVR_Input.GetActionFromPath<SteamVR_Action_Vibration>("/actions/default/out/Haptic"); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
} | |
// slight vibrations on collision | |
private void OnCollisionEnter(UnityEngine.Collision collision) | |
{ | |
Debug.Log("Controller collision entering" + collision.transform.name); | |
inCollision = true; | |
colliderName = collision.transform.name; | |
if (gameObject.name.Contains("right")) | |
hapticAction.Execute(0.0f, 0.05f, 50.0f, 0.1f, SteamVR_Input_Sources.RightHand); | |
if (gameObject.name.Contains("left")) | |
hapticAction.Execute(0.0f, 0.05f, 50.0f, 0.1f, SteamVR_Input_Sources.LeftHand); | |
} | |
private void OnCollisionExit(UnityEngine.Collision collision) | |
{ | |
Debug.Log("Controller collision exiting" + collision.transform.name); | |
inCollision = false; // collision complete | |
colliderName = ""; | |
if (gameObject.name.Contains("right")) | |
hapticAction.Execute(0.0f, 0.05f, 50.0f, 0.1f, SteamVR_Input_Sources.RightHand); | |
if (gameObject.name.Contains("left")) | |
hapticAction.Execute(0.0f, 0.05f, 50.0f, 0.1f, SteamVR_Input_Sources.LeftHand); | |
} | |
public bool IsColliding() | |
{ | |
return inCollision; | |
} | |
public string CollidingWith() | |
{ | |
return colliderName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment