Basic oject interaction login
This commit is contained in:
parent
d216580bca
commit
2d2eb7d32f
8 changed files with 370 additions and 134 deletions
13
Assets/Scripts/ClearCounter.cs
Normal file
13
Assets/Scripts/ClearCounter.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ClearCounter : MonoBehaviour
|
||||
{
|
||||
|
||||
public void Interact()
|
||||
{
|
||||
Debug.Log("Interacted!");
|
||||
}
|
||||
|
||||
}
|
11
Assets/Scripts/ClearCounter.cs.meta
Normal file
11
Assets/Scripts/ClearCounter.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bb159edc225081c408fe7a01d52732e4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -7,12 +7,15 @@ public class Player : MonoBehaviour
|
|||
|
||||
[SerializeField] private float moveSpeed = 7f;
|
||||
[SerializeField] private GameInput gameInput;
|
||||
[SerializeField] private LayerMask countersLayerMask;
|
||||
|
||||
private bool isWalking;
|
||||
private Vector3 lastInteractDir;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
HandleMovement();
|
||||
HandleInteractions();
|
||||
}
|
||||
|
||||
public bool IsWalking()
|
||||
|
@ -20,6 +23,36 @@ public class Player : MonoBehaviour
|
|||
return isWalking;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle object interactions.
|
||||
/// </summary>
|
||||
private void HandleInteractions()
|
||||
{
|
||||
Vector2 inputVector = gameInput.GetMovementVectorNormalized();
|
||||
|
||||
Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y);
|
||||
|
||||
// Kepp a constantly updating record of the last move direction.
|
||||
// This means you can interact without holding down a direction.
|
||||
if (moveDir != Vector3.zero)
|
||||
{
|
||||
lastInteractDir = moveDir;
|
||||
}
|
||||
|
||||
float interactDistance = 2f;
|
||||
if (Physics.Raycast(transform.position, lastInteractDir, out RaycastHit raycastHit, interactDistance, countersLayerMask))
|
||||
{
|
||||
if (raycastHit.transform.TryGetComponent(out ClearCounter clearCounter))
|
||||
{
|
||||
// Has Clear Counter
|
||||
clearCounter.Interact();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle the player movement and collision.
|
||||
/// </summary>
|
||||
private void HandleMovement()
|
||||
{
|
||||
Vector2 inputVector = gameInput.GetMovementVectorNormalized();
|
||||
|
|
Reference in a new issue