Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Collision Detection

Discussion in 'Scripting' started by paulmac, Mar 5, 2011.

  1. paulmac

    paulmac

    Joined:
    Feb 17, 2011
    Posts:
    10
    Im new at untty

    I have 2 asteroids which when they randomly bump into each other i want them to move apart again yet I cant even get a simple test oncollisionfunction to work

    example
    (the asteroids have rigidbodies and isKinematic is false.)

    function OnCollisionEnter (collision : Collision)
    {
    print("collided with "+collision.gameObject.tag)
    }

    is it something to do with the move function of the asteroids, I am currently moving the asteroids using

    function Update ()
    {
    transform.Translate(Vector3.up * speed * Time.deltaTime);
    }


    but I also have tried:-

    function FixedUpdate ()
    {
    rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
    }

    Is there something Im missing ?
     
  2. Myx

    Myx

    Joined:
    Nov 29, 2010
    Posts:
    196
    Hello there!

    If you want to directly move the positions, use Kinematic bodies. Otherwise move the bodies with forces, rigidbody.AddForce() would be the method to look at in this case.
     
  3. paulmac

    paulmac

    Joined:
    Feb 17, 2011
    Posts:
    10
    Does using addforce make any difference to the OnCollisionEnter function. I have tried putting addforce into the Fixedupdate function of the asteroid but the asteroid then just keeps gathering more and more speed. I have therefore put it into the Start function but the asteroid doesnt move at all.

    I am currently using transform.translate within the update function (as per my earlier post). So as Addforce didnt seem to be doingthe job i went back to this method and I tried switching Kinematic to true, but it doesnt make any difference. The onCollisionEnter function still fails to Fire.

    using OntriggerEnter works fine but I really need the information that only OnCollisionEnter can provide.

    Update: Using rigidbody.moveposition in the Fixedupdate function has now got things moving but still no collisions are occurring, grrr
     
    Last edited: Mar 5, 2011
  4. paulmac

    paulmac

    Joined:
    Feb 17, 2011
    Posts:
    10
    Can anyone explain to me the difference between rigidbody.moveposition and transform.Translate. If i simply want to move a gameobject I normally use transform.Translate but it seems if I want to check for OnCollisionEnter I must move my gameobject using rigidbody.Moveposition.

    I have 2 problems with this, 1. as explained above nothing I do seems to create a Collision 2. When moving a gameobject using Moveposition the object is not in the position I expect it to be, it seems to be lagging behind. What exactly am I moving with Moveposition, is it the gameobject ? or just the Collider ? this is frustrating, been at it all day saturday :(

    Im simply trying to get 2 bubbles to notice that they have collided so that i can bounce them away from each other.

    Here is the code

    var bubbleSpeed : Vector3 ;

    function Start()
    {
    bubbleSpeed= Vector3(0,Random.Range(1.0,10.0),0);
    rigidbody.MovePosition(rigidbody.position + bubbleSpeed * Time.deltaTime);
    }

    function FixedUpdate ()
    {

    rigidbody.MovePosition(rigidbody.position + bubbleSpeed * Time.deltaTime); //move the bubble up the screen
    transform.Translate(bubbleSpeed * Time.deltaTime); // move the bubble up the screen (lol I know im doing this twice but some stupid issue means i have to move the rigidbody and the transform)
    transform.Rotate( bubbleSpeed* Time.deltaTime); //slowly rotate the bubble

    // check for top of screen
    if (transform.position.y > 50)
    {
    ResetBubble ();
    }
    }

    function ResetBubble()
    {
    //Reset the position of the bubble back to bottom of screen
    var sphereColor : Color;

    rigidbody.position.y = 2;
    rigidbody.position.x = Random.Range(15.0,33.0);

    bubbleSpeed= Vector3(0,Random.Range(1.0,10.0),0);

    rigidbody.MovePosition(rigidbody.position + bubbleSpeed * Time.deltaTime);

    }


    function OnCollisionEnter (collision : Collision)

    { //why isnt this colliding ??????
    Debug.Log("collided with "+collision.gameObject.tag);

    }

     
    Last edited: Mar 5, 2011
  5. Hodofca

    Hodofca

    Joined:
    May 9, 2010
    Posts:
    83
    Your sphere collider have "is trigger" option turned on, so instead of "function OnCollisionEnter " you need to use "function OnTriggerEnter " or just turn off "is trigger". I guess :)