campfire/Assets/Scripts/Item.cs

59 lines
2.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Item : MonoBehaviour {
public BurnQuality Quality;
public Transform IgnitePoint;
[Header("Runtime values")]
public bool Grabbed = false;
private Rigidbody Body;
private Transform World;
private bool BeingPlaced = false;
private Vector3 TargetPosition;
private Vector3 GrabOffset;
private Quaternion GrabRotation;
private void Awake() {
Body = GetComponent<Rigidbody>();
World = GameObject.FindGameObjectWithTag("World").transform;
}
private void Update() {
if (Grabbed) {
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);
}
}
}
public void PickUp(Transform handTransform) {
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);
Body.isKinematic = true;
Grabbed = true;
}
public void Drop(Vector3 where) {
transform.parent = World;
TargetPosition = where;
BeingPlaced = true;
Grabbed = false;
}
}