Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PlayerTrigger component #31

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,50 @@ The player module contains services, utilities and prefabs around the user/playe

Make sure to always use the same source for all toolkit modules. Avoid using different installation sources within the same project. We provide the following ways to install Reality Toolkit modules:

### Method 1: Using Package Manager for git users
### Method 1: OpenUPM CLI

1. Open the Package Manager using the Window menu -> Package Manager

2. Inside the Package Manager, click on the "+" button on the top left and select "Add package from git URL..."

3. Input the following URL: https://github.com/realitycollective/com.realitytoolkit.player.git and click "Add".

### Method 2: OpenUPM
This method requires the [OpenUPM CLI](https://openupm.com/#get-started-with-cli-optional) to be installed on your computer.

```text
openupm add com.realitytoolkit.player
```

### Method 3: Unity Asset Store
### Method 2: OpenUPM Scoped Registry

If you do not wish to use the [OpenUPM CLI](https://openupm.com/#get-started-with-cli-optional) you can also manually add the OpenUPM registry to your project and browse all available toolkit packages.

1. Open the [Project Settings](https://docs.unity3d.com/Manual/comp-ManagerGroup.html) window.

2. Select the **Package Manager** settings category to the left.

3. Add a new scoped registry
1. Name: **OpenUPM**
2. URL: **https://package.openupm.com**
3. Scopes: **com.realitycollective** and **com.realitytoolkit**
4. Press **Save**

![Add Scoped Registry](https://github.com/realitycollective/realitycollective.logo/blob/main/RealityToolkit/ReadmeAssets/add-scoped-registry.png?raw=true)

4. Open the [Package Manager](https://docs.unity3d.com/Manual/Packages.html) window.

5. In the top left packages filter dropdown select **My Registries**.

6. You'll now see all published toolkit packages listed for you to install.

![Add Scoped Registry](https://github.com/realitycollective/realitycollective.logo/blob/main/RealityToolkit/ReadmeAssets/package-manager-registry.png?raw=true)

### Method 3: Using Package Manager for git users

1. Open the [Package Manager](https://docs.unity3d.com/Manual/Packages.html) through this menu: **Window -> Package Manager**.

2. Inside the [Package Manager](https://docs.unity3d.com/Manual/Packages.html), click on the **+** button on the top left and select **Add package from git URL...**.

3. Input the following URL: https://github.com/realitycollective/com.realitytoolkit.player.git and click **Add**.

### Method 4: Unity Asset Store

This option will be available soon.
We are working on making the Reality Toolkit available via the Unity asset store and will share news, once available.

## Getting Started

Check the ["Getting Started"](https://www.realitytoolkit.io/) documentation for the Reality Toolkit and to learn more about this module.
Check the [Getting Started](https://www.realitytoolkit.io/) documentation for the Reality Toolkit and to learn more about this module.
93 changes: 93 additions & 0 deletions Runtime/UX/PlayerTrigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) Reality Collective. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using RealityToolkit.Player.Rigs;
using UnityEngine;
using UnityEngine.Events;

namespace RealityToolkit.Player.UX
{
/// <summary>
/// A simple utility component that will raise events as the player enters, leaves or stays within a trigger zone.
/// </summary>
[HelpURL("https://www.realitytoolkit.io/docs/category/player")]
[RequireComponent(typeof(Collider))]
public class PlayerTrigger : MonoBehaviour
{
[SerializeField, Tooltip("Raised, when the player enters the trigger zone.")]
private UnityEvent onPlayerEnter = null;

[Space, SerializeField, Tooltip("Raised, while the player is within the trigger zone.")]
private UnityEvent onPlayerStay = null;

[Space, SerializeField, Tooltip("Raised, when the player leaves the trigger zone.")]
private UnityEvent onPlayerExit = null;

/// <summary>
/// Raised, when the player enters the trigger zone.
/// </summary>
public UnityEvent OnPlayerEnter => onPlayerEnter;

/// <summary>
/// Raised, while the player is within the trigger zone.
/// </summary>
public UnityEvent OnPlayerStay => onPlayerStay;

/// <summary>
/// Raised, when the player leaves the trigger zone.
/// </summary>
public UnityEvent OnPlayerExit => onPlayerExit;

/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
private void Awake()
{
var collider = GetComponent<Collider>();
if (!collider.isTrigger)
{
collider.isTrigger = true;
Debug.LogWarning($"{nameof(PlayerTrigger)} requires the attached {nameof(Collider)} to be a trigger and has auto configured it.", this);
}
}

/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
private void OnTriggerEnter(Collider other)
{
if (!other.TryGetComponent<IPlayerRig>(out _))
{
return;
}

OnPlayerEnter?.Invoke();
}

/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
private void OnTriggerStay(Collider other)
{
if (!other.TryGetComponent<IPlayerRig>(out _))
{
return;
}

OnPlayerStay?.Invoke();
}

/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
private void OnTriggerExit(Collider other)
{
if (!other.TryGetComponent<IPlayerRig>(out _))
{
return;
}

OnPlayerExit?.Invoke();
}
}
}
11 changes: 11 additions & 0 deletions Runtime/UX/PlayerTrigger.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.