1
\$\begingroup\$

I have an issue with my script, I want to make rb.isKinematic go from true to false when collision is detected. With this script, nothing happens when a supposed collision is happening which means that rb.Kinematic = false does not work... The problem is that collision CANNOT be detected in the first place because rb.isKinematic is initially true (in my other script)! I checked this using Debug.Log to detect collisions. How can I fix this problem?

string lastTagCollided = "";
public Rigidbody rb;

void OnCollisionEnter(Collision col)
{
    if(col.gameObject.tag == "Pentagon" && lastTagCollided == "Pyramid")
    {
    rb = GetComponent<Rigidbody>();
    rb.isKinematic = false;
    transform.DetachChildren();
    Destroy (GameObject.FindWithTag("Sphere"));
    }

if(col.gameObject.tag == "Pyramid" && lastTagCollided == "Pentagon")
{
    rb = GetComponent<Rigidbody>();
    rb.isKinematic = false;
    transform.DetachChildren();
    Destroy (GameObject.FindWithTag("Sphere"));
}

lastTagCollided = col.gameObject.tag;
}

My other script if you are curious:

void OnCollisionEnter(Collision col) 
{ 
if (col.gameObject.tag == "Pyramid" || col.gameObject.tag == "Pentagon") 
    {
        rb = GetComponent<Rigidbody>();
        rb.isKinematic = true;
        gameObject.transform.SetParent (col.gameObject.transform);
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ On first sight, you may want to check the OR (||) operator in your logical statement of the second function. \$\endgroup\$
    – Majte
    Oct 24, 2015 at 12:09
  • \$\begingroup\$ It sounds like you need to learn how to debug control flow. A good start is to add a print statement inside the conditional that you think should be firing and check if it's executed, or to learn how to use the debugger and set a breakpoint. \$\endgroup\$
    – Anko
    Oct 24, 2015 at 15:22

1 Answer 1

0
\$\begingroup\$
using UnityEngine;
using System.Collections;

public class CollisionDestroyer: MonoBehaviour {
    public AudioClip clip;
    public GameObject Bullet;

    // Use this for initialization
    void OnCollisionEnter(Collision theCollision){
        if(theCollision.gameObject.tag == "Bullet"){
            AudioSource.PlayClipAtPoint(clip, Vector3.zero, 1.0f); 
            Bullet.GetComponent<Rigidbody>().isKinematic = true;
            Bullet.GetComponent<Rigidbody>().useGravity = false;
            Bullet.GetComponent<Collider>().isTrigger = false;
        }
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .