diff --git a/Assets/Scripts/Child.cs b/Assets/Scripts/Child.cs index 1782642..c3dcba8 100644 --- a/Assets/Scripts/Child.cs +++ b/Assets/Scripts/Child.cs @@ -55,19 +55,23 @@ public class Child : MonoBehaviour void OnTriggerEnter(Collider other) { if (other.tag == "Pillow"){ - - // picking up a pillow - if (this.pillow == null && !other.GetComponent().IsThrown) { - pillow = other.GetComponent(); - other.transform.parent = transform; // make the pillow a child of Child - other.transform.localPosition = other.transform.position + transform.forward; - other.GetComponent().enabled = false; + Pillow incomingPillow = other.GetComponent(); + + // picking up a pillow + if (this.pillow == null && !incomingPillow.IsThrown) { + + pillow = incomingPillow; + + pillow.transform.parent = transform; // make the pillow a child of Child + pillow.transform.localPosition = new Vector3(0f, 1.5f, 0f); + pillow.GetComponent().isKinematic = true; // dont make pillow obey to gravity when in a child's hands + // TODO: place the pillow correctly or animate or something... } // getting hit by a pillow - else if (other.GetComponent().IsThrown) { + else if (incomingPillow.IsThrown) { //player is hit Debug.Log("Child is hit by a pillow"); @@ -169,23 +173,24 @@ public class Child : MonoBehaviour } internal void Throw() { - Vector3 direction; - if (target != null) { - direction = target.transform.position - pillow.transform.position; + + if (pillow != null) { + + Vector3 direction; + + if (target != null) { + direction = target.transform.position - pillow.transform.position; + } + else { + direction = transform.forward; + } + direction = direction.normalized; + + + pillow.Throw(direction * ThrowForce); + + pillow = null; } - else { - direction = transform.forward; - } - direction = direction.normalized; - - - pillow.IsThrown = true; - pillow.transform.parent = null; // detach the pillow from the child object - pillow.GetComponent().isKinematic = false; - pillow.GetComponent().enabled = true; - pillow.GetComponent().AddForce(direction * ThrowForce, ForceMode.Impulse); - - pillow = null; } diff --git a/Assets/Scripts/Pillow.cs b/Assets/Scripts/Pillow.cs index e9fb8ee..09a0246 100644 --- a/Assets/Scripts/Pillow.cs +++ b/Assets/Scripts/Pillow.cs @@ -4,17 +4,51 @@ using System.Collections; public class Pillow : MonoBehaviour { public bool IsThrown = false; + + private bool IsPickable = true; + + private Collider _col; + private Rigidbody _rb; + // Use this for initialization void Start () { - + _col = GetComponent(); + _rb = GetComponent(); } // Update is called once per frame void Update () { - if (transform.position.y < -10) { + + if (transform.position.y < -1) { Destroy(this.gameObject); } } + + void OnCollisionEnter(Collision other) { + if (!IsPickable) { + // on first collision, revert the pillow as pickable + MakePickable(); + } + } + + + public void Throw(Vector3 force) { + IsThrown = true; + IsPickable = false; + transform.parent = null; // detach the pillow from the child object + _rb.isKinematic = false; + _col.enabled = true; + _rb.AddForce(force, ForceMode.Impulse); + } + + public void MakePickable() { + IsThrown = false; + IsPickable = true; + + _col.enabled = true; + _rb.isKinematic = false; + } + }