campfire/Assets/Scripts/Item.cs

65 lines
2.2 KiB
C#
Raw Permalink Normal View History

2020-04-18 22:52:37 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Burnable))]
2020-05-01 04:37:20 +02:00
[RequireComponent(typeof(PickupStatus))]
2020-04-18 22:52:37 +02:00
public class Item : MonoBehaviour {
public Transform IgnitePoint;
public BurnQuality Quality {
get {
return Burnable.Quality;
}
}
2020-05-01 04:37:20 +02:00
private PickupStatus Pickup;
private Burnable Burnable;
2020-04-18 22:52:37 +02:00
private Rigidbody Body;
private Transform World;
private bool BeingPlaced = false;
private Vector3 TargetPosition;
2020-04-20 01:39:38 +02:00
private Vector3 GrabOffset;
private Quaternion GrabRotation;
2020-04-18 22:52:37 +02:00
private void Awake() {
Burnable = GetComponent<Burnable>();
2020-04-18 22:52:37 +02:00
Body = GetComponent<Rigidbody>();
2020-05-01 04:37:20 +02:00
Pickup = GetComponent<PickupStatus>();
2020-04-18 22:52:37 +02:00
World = GameObject.FindGameObjectWithTag("World").transform;
}
private void Update() {
2020-05-01 04:37:20 +02:00
if (Pickup.Grabbed) {
2020-04-20 01:39:38 +02:00
transform.localPosition = Vector3.Lerp(transform.localPosition, GrabOffset, 10f * Time.deltaTime);
transform.localRotation = Quaternion.Slerp(transform.localRotation, GrabRotation, 10f * Time.deltaTime);
} else if (BeingPlaced) {
// Lerp to TargetPosition, then continue simulation
Vector3 Delta = TargetPosition - transform.position;
float Diff = Delta.magnitude;
if (Diff < 0.1f) {
transform.position = TargetPosition;
Body.isKinematic = false;
BeingPlaced = false;
} else {
transform.position = Vector3.Lerp(transform.position, TargetPosition, 20f * Time.deltaTime);
}
2020-04-18 22:52:37 +02:00
}
}
public void PickUp(Transform handTransform) {
2020-04-18 22:52:37 +02:00
transform.parent = handTransform;
GrabOffset = new Vector3(Random.value - 0.5f, Random.value - 0.5f, Random.value - 0.5f) * 0.15f;
Vector3 DirectionOffset = new Vector3(Random.value - 0.5f, Random.value - 0.5f, 0) * 0.8f;
GrabRotation = Quaternion.LookRotation(Vector3.down, (Vector3.forward + DirectionOffset).normalized);
2020-04-18 22:52:37 +02:00
Body.isKinematic = true;
2020-05-01 04:37:20 +02:00
Pickup.Grab();
2020-04-18 22:52:37 +02:00
}
public void Drop(Vector3 where) {
2020-04-18 22:52:37 +02:00
transform.parent = World;
TargetPosition = where;
BeingPlaced = true;
2020-04-18 22:52:37 +02:00
}
}