Skip to content

Instantly share code, notes, and snippets.

@charlieamat
Last active September 23, 2022 21:32
Show Gist options
  • Save charlieamat/6d964fb9dabfb8f74b0e0a69b927c553 to your computer and use it in GitHub Desktop.
Save charlieamat/6d964fb9dabfb8f74b0e0a69b927c553 to your computer and use it in GitHub Desktop.
[ta-edu-course-survival-game] Chapter 2 — MonoBehaviours & GameObjects
  • Create Interactor.cs
    • We want to let the Player interact with the environment
    • Or in other words, we want the Player to be an interactor of the environment
  • Create Interactable.cs
    • Interactors needs something to interact with, so let's define a new component called "Interactable"
  • Open Interactable.cs
  • Implement a public method called Interact()
    • The idea here is that Interactors will call this method
  • Add a Debug.Log() statement to Interact()
    • Debug.Log("I've been interacted with!", gameObject)
    • Let's log something to the console to verify that our behavior works when we get it all set up in the editor
  • Open Interactor.cs
  • Implement a public method called OnInteract()
    • We need a way for the player initiate an interaction
    • This method will called by Unity's input system, which is outside the scope of this course
  • Add a private field called _nearestInteractable
    • For our implementation, we're gonna have the interactor keep track of the nearest interactable at all times
  • Add a call to _nearestInteractable.Interact() to OnInteract()
    • So all OnInteract() has to do is call Interact on the nearest interactable
  • Implement the Update() method
    • Update is method that's special to MonoBehaviours, just like Start
    • We're going to cover these special methods in future section
    • For now, just know that Update gets called once a frame
  • Implement the logic to set _nearestInteractable
    • The implementation isn't important here
    • We've actually written an unoptimized version of this code that we'll revisit later
  • Switch back to Unity
  • Drag the Interactor component onto the Player GameObject
  • Add a cube in the scene called "Interactable"
  • Drag the Interactable component onto the Interactable GameObject
  • Run the scene and demonstrate the working behavior
using UnityEngine;
public class Interactable : MonoBehaviour
{
public void Interact()
{
Debug.Log("I've been interacted with!", this);
}
}
using UnityEngine;
public class Interactor : MonoBehaviour
{
private float _minimumDistance = 3;
private Interactable _nearestInteractable;
public void OnInteract()
{
_nearestInteractable?.Interact();
}
private void Update()
{
_nearestInteractalbe = null;
foreach (interactable in FindObjectsOfType<Interactable>())
{
if (Distance(interactable.transform) > _minimumDistance) continue;
if (_nearestInteractable == null || Distance(interactable.transform) < Distance(_nearestInteractable.transform))
{
_nearestInteractable = interactable;
}
}
}
public float Distance(Transform other) => Vector3.Distance(transform.position, other.position)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment