86 lines
2.0 KiB
C#
86 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class Grabbable : MonoBehaviour
|
|
{
|
|
protected Rigidbody rb;
|
|
protected Collider collider;
|
|
|
|
bool wasCollecting = false;
|
|
bool smoothing = false;
|
|
bool lockSmoothing = false;
|
|
bool interactedWith = false;
|
|
|
|
public virtual void OnPlayerCollect() { }
|
|
|
|
private void Awake()
|
|
{
|
|
collider = GetComponent<Collider>();
|
|
rb = GetComponent<Rigidbody>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!Player.Instance) return;
|
|
|
|
if (Vector3.Distance(transform.position, Player.Instance.CustardAnchor.position) < 0.5f || lockSmoothing)
|
|
smoothing = false;
|
|
else
|
|
smoothing = true;
|
|
|
|
bool playerCollecting = Player.Instance.collecting && Player.Instance.InRangeForCollectGrab(gameObject);
|
|
|
|
if (playerCollecting) {
|
|
if (Player.Instance.Collecting == null)
|
|
Player.Instance.Collecting = transform;
|
|
} else
|
|
{
|
|
if (Player.Instance.Collecting == transform)
|
|
Player.Instance.Collecting = null;
|
|
}
|
|
|
|
if (Player.Instance.Collecting == transform)
|
|
{
|
|
if (!smoothing)
|
|
lockSmoothing = true;
|
|
|
|
wasCollecting = true;
|
|
interactedWith = true;
|
|
|
|
transform.position = Vector3.Lerp(transform.position, Player.Instance.CustardAnchor.position, (smoothing) ? Time.deltaTime * 10f : 1);
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, Player.Instance.CustardAnchor.rotation, (smoothing) ? Time.deltaTime * 10f : 1);
|
|
|
|
collider.enabled = false;
|
|
rb.isKinematic = true;
|
|
}
|
|
else
|
|
{
|
|
lockSmoothing = false;
|
|
|
|
if (wasCollecting)
|
|
{
|
|
if (Player.Instance.InRangeForCollect(gameObject))
|
|
OnPlayerCollect();
|
|
}
|
|
wasCollecting = false;
|
|
|
|
collider.enabled = true;
|
|
if (interactedWith)
|
|
rb.isKinematic = false;
|
|
}
|
|
}
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
if (!Player.Instance) return;
|
|
|
|
Gizmos.color = Color.pink;
|
|
Gizmos.DrawWireSphere(transform.position, Player.Instance.CollectGrabRange);
|
|
Gizmos.color = Color.white;
|
|
}
|
|
} |