2020-04-18 22:52:37 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
[RequireComponent(typeof(Rigidbody))]
|
|
|
|
|
public class Item : MonoBehaviour {
|
|
|
|
|
[Header("Runtime values")]
|
|
|
|
|
public bool Grabbed = false;
|
|
|
|
|
|
|
|
|
|
private Rigidbody Body;
|
|
|
|
|
private Transform World;
|
2020-04-19 00:30:27 +02:00
|
|
|
|
private bool BeingPlaced = false;
|
|
|
|
|
private Vector3 TargetPosition;
|
2020-04-18 22:52:37 +02:00
|
|
|
|
|
|
|
|
|
private void Awake() {
|
|
|
|
|
Body = GetComponent<Rigidbody>();
|
|
|
|
|
World = GameObject.FindGameObjectWithTag("World").transform;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update() {
|
|
|
|
|
if (Grabbed) {
|
|
|
|
|
transform.localPosition = Vector3.Lerp(transform.localPosition, Vector3.zero, 10f * Time.deltaTime);
|
2020-04-19 00:30:27 +02:00
|
|
|
|
} 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) {
|
|
|
|
|
transform.parent = handTransform;
|
|
|
|
|
Body.isKinematic = true;
|
|
|
|
|
Grabbed = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 00:30:27 +02:00
|
|
|
|
public void Drop(Vector3 where) {
|
2020-04-18 22:52:37 +02:00
|
|
|
|
transform.parent = World;
|
2020-04-19 00:30:27 +02:00
|
|
|
|
TargetPosition = where;
|
|
|
|
|
BeingPlaced = true;
|
2020-04-18 22:52:37 +02:00
|
|
|
|
Grabbed = false;
|
|
|
|
|
}
|
|
|
|
|
}
|