BodyCollider.cs 1020 B

1234567891011121314151617181920212223242526272829303132333435
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Collider dangling from the player's head
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. [RequireComponent( typeof( CapsuleCollider ) )]
  12. public class BodyCollider : MonoBehaviour
  13. {
  14. public Transform head;
  15. private CapsuleCollider capsuleCollider;
  16. //-------------------------------------------------
  17. void Awake()
  18. {
  19. capsuleCollider = GetComponent<CapsuleCollider>();
  20. }
  21. //-------------------------------------------------
  22. void FixedUpdate()
  23. {
  24. float distanceFromFloor = Vector3.Dot( head.localPosition, Vector3.up );
  25. capsuleCollider.height = Mathf.Max( capsuleCollider.radius, distanceFromFloor );
  26. transform.localPosition = head.localPosition - 0.5f * distanceFromFloor * Vector3.up;
  27. }
  28. }
  29. }