campfire/Assets/Scripts/Item.cs

38 lines
1.1 KiB
C#

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;
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);
transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.identity, 10f * Time.deltaTime);
}
}
public void PickUp(Transform handTransform) {
transform.parent = handTransform;
Body.isKinematic = true;
Grabbed = true;
}
public void Drop(Vector3 throwVector) {
transform.parent = World;
Body.isKinematic = false;
Body.AddForce(throwVector, ForceMode.VelocityChange);
Grabbed = false;
}
}