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 bool BeingPlaced = false;
private Vector3 TargetPosition;
private Vector3 GrabOffset;
private void Awake() {
Body = GetComponent<Rigidbody>();
@ -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;
}

View File

@ -20,7 +20,7 @@ public class ItemGrabber : MonoBehaviour {
public GameObject TorchPrefab;
[Header("Runtime values")]
public Item GrabbedItem;
public List<Item> 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<Item>();
}
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<Item>();
}
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<Campfire>();
}
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<Torch>();
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()}";
}
}
}