From c29da61525253deee59b0d3429c525427018b788 Mon Sep 17 00:00:00 2001 From: Jens Pitkanen Date: Mon, 20 Apr 2020 02:39:38 +0300 Subject: [PATCH] Add multi-stick-carrying --- Assets/Scripts/Item.cs | 6 +++-- Assets/Scripts/ItemGrabber.cs | 45 +++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/Assets/Scripts/Item.cs b/Assets/Scripts/Item.cs index a9b13c1..a00347f 100644 --- a/Assets/Scripts/Item.cs +++ b/Assets/Scripts/Item.cs @@ -11,6 +11,7 @@ public class Item : MonoBehaviour { private Transform World; private bool BeingPlaced = false; private Vector3 TargetPosition; + private Vector3 GrabOffset; private void Awake() { Body = GetComponent(); @@ -19,7 +20,7 @@ public class Item : MonoBehaviour { private void Update() { if (Grabbed) { - transform.localPosition = Vector3.Lerp(transform.localPosition, Vector3.zero, 10f * Time.deltaTime); + transform.localPosition = Vector3.Lerp(transform.localPosition, GrabOffset, 10f * Time.deltaTime); } else if (BeingPlaced) { // Lerp to TargetPosition, then continue simulation Vector3 Delta = TargetPosition - transform.position; @@ -34,8 +35,9 @@ public class Item : MonoBehaviour { } } - public void PickUp(Transform handTransform) { + public void PickUp(Transform handTransform, Vector3 offset) { transform.parent = handTransform; + GrabOffset = offset; Body.isKinematic = true; Grabbed = true; } diff --git a/Assets/Scripts/ItemGrabber.cs b/Assets/Scripts/ItemGrabber.cs index 7118619..b94962b 100644 --- a/Assets/Scripts/ItemGrabber.cs +++ b/Assets/Scripts/ItemGrabber.cs @@ -20,7 +20,7 @@ public class ItemGrabber : MonoBehaviour { public GameObject TorchPrefab; [Header("Runtime values")] - public Item GrabbedItem; + public List GrabbedItems; public Torch Torch; private void Start() { @@ -34,29 +34,32 @@ public class ItemGrabber : MonoBehaviour { RaycastHit Hit; Vector3 From = CameraTransform.position; Vector3 Direction = CameraTransform.forward; - if (GrabbedItem == null) { - Debug.DrawLine(From, From + Direction * Distance, Color.red); - if (Physics.Raycast(From, Direction, out Hit, Distance, ItemLayer) && - Hit.collider.attachedRigidbody != null) { - Item = Hit.collider.attachedRigidbody.GetComponent(); - } - if (Item != null && Input.GetButtonDown("Grab")) { - Item.PickUp(HandTransform); - GrabbedItem = Item; - } - } else { + if (Physics.Raycast(From, Direction, out Hit, Distance, ItemLayer) && + Hit.collider.attachedRigidbody != null) { + Item = Hit.collider.attachedRigidbody.GetComponent(); + } + + if (Item != null && Input.GetButtonDown("Grab")) { + Item.PickUp(HandTransform, new Vector3(Random.value - 0.5f, Random.value - 0.5f, Random.value - 0.5f) * 0.1f); + GrabbedItems.Add(Item); + } + + if (GrabbedItems.Count > 0) { if (Physics.Raycast(From, Direction, out Hit, Distance * 2f, 1 << LayerMask.NameToLayer("Campfire")) && Hit.collider.attachedRigidbody != null) { Campfire = Hit.collider.attachedRigidbody.GetComponent(); } if (Campfire != null && Torch == null && Input.GetButtonDown("Ignite")) { + Item GrabbedItem = GrabbedItems[0]; + GrabbedItems.RemoveAt(0); GameObject TorchObj = Instantiate(TorchPrefab, GrabbedItem.transform.position, GrabbedItem.transform.rotation, TorchHandTransform); Torch = TorchObj.GetComponent(); Destroy(GrabbedItem.gameObject); - GrabbedItem = null; - } else if (Input.GetButtonDown("Grab")) { + } + + if (Input.GetButtonDown("Grab") && Item == null) { Vector3 DropPosition; if (Campfire != null) { DropPosition = Campfire.transform.position + Vector3.up * 0.7f; @@ -67,20 +70,20 @@ public class ItemGrabber : MonoBehaviour { DropPosition = From + Direction * Distance; } } - GrabbedItem.Drop(DropPosition); - GrabbedItem = null; + GrabbedItems[0].Drop(DropPosition); + GrabbedItems.RemoveAt(0); } } PrimaryIndicator.alpha = Mathf.Lerp(PrimaryIndicator.alpha, Item != null || Campfire != null ? 1 : 0, 10f * Time.deltaTime); - if (Campfire != null && GrabbedItem != null) { - PrimaryText.text = $"Burn {GrabbedItem.name.ToLower()}"; + if (Campfire != null && GrabbedItems.Count > 0) { + PrimaryText.text = $"Burn {GrabbedItems[0].name.ToLower()}"; } else if (Item != null) { PrimaryText.text = $"Take {Item.name.ToLower()}"; } - SecondaryIndicator.alpha = Mathf.Lerp(SecondaryIndicator.alpha, Campfire != null && GrabbedItem != null && Torch == null ? 1 : 0, 10f * Time.deltaTime); - if (GrabbedItem != null) { - SecondaryText.text = $"Ignite {GrabbedItem.name.ToLower()}"; + SecondaryIndicator.alpha = Mathf.Lerp(SecondaryIndicator.alpha, Campfire != null && GrabbedItems.Count > 0 && Torch == null ? 1 : 0, 10f * Time.deltaTime); + if (GrabbedItems.Count > 0) { + SecondaryText.text = $"Ignite {GrabbedItems[0].name.ToLower()}"; } } }