Skip to content
Snippets Groups Projects
Commit c6725b02 authored by Falampe's avatar Falampe
Browse files

added wall collider object to teacher in media room

parent 32f272a2
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is stored in LFS. Options to address this: view the blob.
using System.Threading.Tasks;
using TeachR.ApplicationManager;
using UnityEngine;
public class WallOffset : MonoBehaviour
{
/*
* This Class will establish a Collider sorrounding the XRRig that detects the walls and prevents the player from walking through them by stopping the movement in the direction of the wall.
*/
public GameObject XRRig;
void Awake()
{
InitGlobals.LogicInitHook += EnsureColliderOnXRRig;
}
private Task EnsureColliderOnXRRig()
{
if (XRRig != null && XRRig.GetComponent<Collider>() == null)
{
SphereCollider col = XRRig.AddComponent<SphereCollider>();
col.radius = 0.5f;
}
return Task.CompletedTask;
}
private void OnCollisionStay(Collision collision)
{
// berprfen, ob das kollidierende Objekt das Tag "wall" hat
if (collision.gameObject.CompareTag("wall"))
{
Vector3 totalNormal = Vector3.zero;
// Summe der Normalenvektoren aller Kontaktpunkte berechnen
foreach (ContactPoint contact in collision.contacts)
{
totalNormal += contact.normal;
}
// Normalisieren des resultierenden Normalenvektors
totalNormal.Normalize();
// Berechnung der Bewegungsrichtung des XRRig relativ zur Kollision
Vector3 movement = XRRig.transform.position - collision.transform.position;
Vector3 movementDirection = movement.normalized;
// Berechnung der neuen Richtung basierend auf der Reflexion der Bewegungsrichtung an der Wand
Vector3 newDirection = Vector3.Reflect(movementDirection, totalNormal);
// Sicherstellen, dass die neue Position das XRRig nicht durch die Wand bewegt
Vector3 potentialNewPosition = XRRig.transform.position + newDirection * Time.deltaTime;
if (!Physics.Raycast(XRRig.transform.position, newDirection, out RaycastHit hit, newDirection.magnitude * Time.deltaTime) || hit.collider != collision.collider)
{
// Aktualisieren der Position des XRRig auf die neue berechnete Position
XRRig.transform.position = potentialNewPosition;
}
}
}
}
fileFormatVersion: 2
guid: a807e1003469e8349ad4334bbe5ddc5b
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment