102 lines
2.4 KiB
C#
102 lines
2.4 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
public class DW : MonoBehaviour
|
|
{
|
|
public GameObject objectToCarry;
|
|
public Transform handTransform;
|
|
public Animator characterAnimator;
|
|
public LayerMask groundLayer;
|
|
private bool isCarrying = false;
|
|
private float pickupDelay = 2.0f;
|
|
public float placeDelay = 4.0f;
|
|
public float placeOnGroundDelay = 6.0f;
|
|
private Coroutine pickupCoroutine;
|
|
|
|
void Start()
|
|
{
|
|
|
|
pickupCoroutine = StartCoroutine(PickupAfterDelay(pickupDelay));
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
|
|
if (Input.GetKeyDown(KeyCode.E) && isCarrying)
|
|
{
|
|
StopCoroutine(pickupCoroutine);
|
|
PlaceObject();
|
|
}
|
|
}
|
|
|
|
|
|
public void SetPickupDelay(float newDelay)
|
|
{
|
|
pickupDelay = newDelay;
|
|
Quaternion specificRotation = Quaternion.Euler(-4, -17, 32);
|
|
objectToCarry.transform.localRotation = specificRotation;
|
|
|
|
if (pickupCoroutine != null)
|
|
{
|
|
|
|
StopCoroutine(pickupCoroutine);
|
|
pickupCoroutine = StartCoroutine(PickupAfterDelay(pickupDelay));
|
|
}
|
|
}
|
|
|
|
IEnumerator PickupAfterDelay(float delay)
|
|
{
|
|
yield return new WaitForSeconds(delay);
|
|
|
|
|
|
StartCarrying();
|
|
}
|
|
|
|
void StartCarrying()
|
|
{
|
|
isCarrying = true;
|
|
objectToCarry.transform.SetParent(handTransform);
|
|
objectToCarry.transform.localPosition = Vector3.zero;
|
|
characterAnimator.SetTrigger("carry");
|
|
|
|
StartCoroutine(PlaceAfterDelay(placeDelay));
|
|
}
|
|
|
|
IEnumerator PlaceAfterDelay(float delay)
|
|
{
|
|
yield return new WaitForSeconds(delay);
|
|
|
|
|
|
PlaceObject();
|
|
|
|
|
|
StartCoroutine(PlaceOnGroundAfterDelay(placeOnGroundDelay));
|
|
}
|
|
|
|
void PlaceObject()
|
|
{
|
|
isCarrying = false;
|
|
objectToCarry.transform.SetParent(null);
|
|
characterAnimator.SetTrigger("release");
|
|
}
|
|
|
|
IEnumerator PlaceOnGroundAfterDelay(float delay)
|
|
{
|
|
yield return new WaitForSeconds(delay);
|
|
|
|
|
|
PlaceObjectOnGround();
|
|
}
|
|
|
|
void PlaceObjectOnGround()
|
|
{
|
|
RaycastHit hit;
|
|
|
|
if (Physics.Raycast(objectToCarry.transform.position, -Vector3.up, out hit, 10f, groundLayer))
|
|
{
|
|
objectToCarry.transform.position = hit.point;
|
|
objectToCarry.transform.rotation = Quaternion.identity;
|
|
}
|
|
}
|
|
}
|