worked on interactions

This commit is contained in:
Rizvee committed 2022-02-23 02:16:40 -06:00
1 parent e4bb5027a5
commit 8b5c99ae8d
9 files changed
+259 -11

No files matched your search

+66
View File
@@ -0,0 +1,66 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//to implement inheritance
public abstract class Trigger : MonoBehaviour
{
//tags for the trigger
public bool forceInteract;
public bool repeatable;
bool isActive;
//only worry about this if it is not repeatable
bool hasOccurred;
public int value;
// Start is called before the first frame update
void Start()
{
hasOccurred = false;
}
//checks if player is in range of the trigger
private void OnTriggerEnter2D(Collider2D other) {
if(other.CompareTag("Player")) {
isActive = true;
}
}
private void OnTriggerExit2D(Collider2D other) {
if(other.CompareTag("Player")) {
isActive = false;
}
if(forceInteract && repeatable) {
hasOccurred = false;
}
}
// Update is called once per frame
void Update()
{
//checks if conditions are right
if(isActive && !hasOccurred)
{
//executes action upon entering
if(forceInteract)
{
Action();
hasOccurred = true;
isActive = false;
}
//only executes action if input is pressed
else
{
if(Input.GetKeyDown(KeyCode.E)) {
Action();
if(!repeatable)
{
hasOccurred = true;
}
}
}
}
}
public abstract void Action();
}