make pillows pickable after being thrown

This commit is contained in:
jparent 2015-08-23 01:40:55 -04:00
parent dcbf2df911
commit bb398ce257
2 changed files with 65 additions and 26 deletions

View File

@ -55,19 +55,23 @@ public class Child : MonoBehaviour
void OnTriggerEnter(Collider other) { void OnTriggerEnter(Collider other) {
if (other.tag == "Pillow"){ if (other.tag == "Pillow"){
// picking up a pillow
if (this.pillow == null && !other.GetComponent<Pillow>().IsThrown) {
pillow = other.GetComponent<Pillow>(); Pillow incomingPillow = other.GetComponent<Pillow>();
other.transform.parent = transform; // make the pillow a child of Child
other.transform.localPosition = other.transform.position + transform.forward; // picking up a pillow
other.GetComponent<Collider>().enabled = false; 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<Rigidbody>().isKinematic = true; // dont make pillow obey to gravity when in a child's hands
// TODO: place the pillow correctly or animate or something... // TODO: place the pillow correctly or animate or something...
} }
// getting hit by a pillow // getting hit by a pillow
else if (other.GetComponent<Pillow>().IsThrown) { else if (incomingPillow.IsThrown) {
//player is hit //player is hit
Debug.Log("Child is hit by a pillow"); Debug.Log("Child is hit by a pillow");
@ -169,23 +173,24 @@ public class Child : MonoBehaviour
} }
internal void Throw() { internal void Throw() {
Vector3 direction;
if (target != null) { if (pillow != null) {
direction = target.transform.position - pillow.transform.position;
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<Rigidbody>().isKinematic = false;
pillow.GetComponent<Collider>().enabled = true;
pillow.GetComponent<Rigidbody>().AddForce(direction * ThrowForce, ForceMode.Impulse);
pillow = null;
} }

View File

@ -4,17 +4,51 @@ using System.Collections;
public class Pillow : MonoBehaviour { public class Pillow : MonoBehaviour {
public bool IsThrown = false; public bool IsThrown = false;
private bool IsPickable = true;
private Collider _col;
private Rigidbody _rb;
// Use this for initialization // Use this for initialization
void Start () { void Start () {
_col = GetComponent<Collider>();
_rb = GetComponent<Rigidbody>();
} }
// Update is called once per frame // Update is called once per frame
void Update () { void Update () {
if (transform.position.y < -10) {
if (transform.position.y < -1) {
Destroy(this.gameObject); 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;
}
} }