Add multi-stick-carrying

This commit is contained in:
Jens Pitkänen 2020-04-20 02:39:38 +03:00
parent e9601791aa
commit c29da61525
2 changed files with 28 additions and 23 deletions

View File

@ -11,6 +11,7 @@ public class Item : MonoBehaviour {
private Transform World; private Transform World;
private bool BeingPlaced = false; private bool BeingPlaced = false;
private Vector3 TargetPosition; private Vector3 TargetPosition;
private Vector3 GrabOffset;
private void Awake() { private void Awake() {
Body = GetComponent<Rigidbody>(); Body = GetComponent<Rigidbody>();
@ -19,7 +20,7 @@ public class Item : MonoBehaviour {
private void Update() { private void Update() {
if (Grabbed) { 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) { } else if (BeingPlaced) {
// Lerp to TargetPosition, then continue simulation // Lerp to TargetPosition, then continue simulation
Vector3 Delta = TargetPosition - transform.position; 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; transform.parent = handTransform;
GrabOffset = offset;
Body.isKinematic = true; Body.isKinematic = true;
Grabbed = true; Grabbed = true;
} }

View File

@ -20,7 +20,7 @@ public class ItemGrabber : MonoBehaviour {
public GameObject TorchPrefab; public GameObject TorchPrefab;
[Header("Runtime values")] [Header("Runtime values")]
public Item GrabbedItem; public List<Item> GrabbedItems;
public Torch Torch; public Torch Torch;
private void Start() { private void Start() {
@ -34,29 +34,32 @@ public class ItemGrabber : MonoBehaviour {
RaycastHit Hit; RaycastHit Hit;
Vector3 From = CameraTransform.position; Vector3 From = CameraTransform.position;
Vector3 Direction = CameraTransform.forward; 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<Item>();
}
if (Item != null && Input.GetButtonDown("Grab")) { if (Physics.Raycast(From, Direction, out Hit, Distance, ItemLayer) &&
Item.PickUp(HandTransform); Hit.collider.attachedRigidbody != null) {
GrabbedItem = Item; Item = Hit.collider.attachedRigidbody.GetComponent<Item>();
} }
} else {
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")) && if (Physics.Raycast(From, Direction, out Hit, Distance * 2f, 1 << LayerMask.NameToLayer("Campfire")) &&
Hit.collider.attachedRigidbody != null) { Hit.collider.attachedRigidbody != null) {
Campfire = Hit.collider.attachedRigidbody.GetComponent<Campfire>(); Campfire = Hit.collider.attachedRigidbody.GetComponent<Campfire>();
} }
if (Campfire != null && Torch == null && Input.GetButtonDown("Ignite")) { 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); GameObject TorchObj = Instantiate(TorchPrefab, GrabbedItem.transform.position, GrabbedItem.transform.rotation, TorchHandTransform);
Torch = TorchObj.GetComponent<Torch>(); Torch = TorchObj.GetComponent<Torch>();
Destroy(GrabbedItem.gameObject); Destroy(GrabbedItem.gameObject);
GrabbedItem = null; }
} else if (Input.GetButtonDown("Grab")) {
if (Input.GetButtonDown("Grab") && Item == null) {
Vector3 DropPosition; Vector3 DropPosition;
if (Campfire != null) { if (Campfire != null) {
DropPosition = Campfire.transform.position + Vector3.up * 0.7f; DropPosition = Campfire.transform.position + Vector3.up * 0.7f;
@ -67,20 +70,20 @@ public class ItemGrabber : MonoBehaviour {
DropPosition = From + Direction * Distance; DropPosition = From + Direction * Distance;
} }
} }
GrabbedItem.Drop(DropPosition); GrabbedItems[0].Drop(DropPosition);
GrabbedItem = null; GrabbedItems.RemoveAt(0);
} }
} }
PrimaryIndicator.alpha = Mathf.Lerp(PrimaryIndicator.alpha, Item != null || Campfire != null ? 1 : 0, 10f * Time.deltaTime); PrimaryIndicator.alpha = Mathf.Lerp(PrimaryIndicator.alpha, Item != null || Campfire != null ? 1 : 0, 10f * Time.deltaTime);
if (Campfire != null && GrabbedItem != null) { if (Campfire != null && GrabbedItems.Count > 0) {
PrimaryText.text = $"Burn {GrabbedItem.name.ToLower()}"; PrimaryText.text = $"Burn {GrabbedItems[0].name.ToLower()}";
} else if (Item != null) { } else if (Item != null) {
PrimaryText.text = $"Take {Item.name.ToLower()}"; PrimaryText.text = $"Take {Item.name.ToLower()}";
} }
SecondaryIndicator.alpha = Mathf.Lerp(SecondaryIndicator.alpha, Campfire != null && GrabbedItem != null && Torch == null ? 1 : 0, 10f * Time.deltaTime); SecondaryIndicator.alpha = Mathf.Lerp(SecondaryIndicator.alpha, Campfire != null && GrabbedItems.Count > 0 && Torch == null ? 1 : 0, 10f * Time.deltaTime);
if (GrabbedItem != null) { if (GrabbedItems.Count > 0) {
SecondaryText.text = $"Ignite {GrabbedItem.name.ToLower()}"; SecondaryText.text = $"Ignite {GrabbedItems[0].name.ToLower()}";
} }
} }
} }