Search Unity

resetting a gameobject after a collision

Discussion in 'Scripting' started by galent, Jun 8, 2009.

  1. galent

    galent

    Joined:
    Jan 7, 2008
    Posts:
    1,078
    Hi all,

    well... *sigh*, I trying to reuse several gameObjects during play. Basically, after the object has been shot I try and reset the object (which I'm storing in an array) and reuse it. Well, everything was working beautifully, shot the object, it popped up into the air rotating like a charm, then ... all of a sudden I can't seem to reset it! The object has a rigidbody, no drag at all, a constant force component (which does more or less appear to reset) and a box collider. what I want is a complete reset. Unfortunately... not happening!

    Here's what I've tried thus far:

    Code (csharp):
    1. function ResetMe() {
    2.     move = false;  
    3.     gameObject.SetActiveRecursively(false);
    4.     //gameObject.rigidbody.Sleep();
    5.     gameObject.rigidbody.rotation = Quaternion.identity;
    6.    
    7.     //rigidbody.isKinematic = true;
    8.     gameObject.constantForce.enabled = false;
    9.     rigidbody.velocity = Vector3.zero;
    10.     gameObject.constantForce.force = Vector3.zero;
    11.     gameObject.constantForce.torque = Vector3.zero;
    12.     gameObject.constantForce.enabled = true;
    13.     gameObject.transform.position = Vector3(0,0,0);
    14.     gameObject.transform.rotation = Quaternion.identity;
    15.     gameObject.transform.localPosition = Vector3(0,0,0);   
    16.     gameObject.transform.localRotation = Quaternion.identity;
    17.     //rigidbody.freezeRotation = true;
    18.     //rigidbody.freezeRotation = false;
    19.     collider.isTrigger = false;
    20.     //rigidbody.isKinematic = false;
    21. }
    Anyone have any idea how to get it to stop acting on physics/rotation ??

    Thanks,

    Galen
     
  2. Apache

    Apache

    Joined:
    Sep 26, 2008
    Posts:
    134
    Have you already tried something like this?

    Code (csharp):
    1.  
    2. var startPos;
    3. var startRot;
    4.  
    5. void Start () {
    6.    startPos = transform.position; // or localPosition, depends to the use you make in the scene
    7.    startRot = transform.rotation; // or localRotation, depends to the use you make in the scene
    8. } // Start
    9.  
    10. void resetMe () {
    11.    rigidbody.velocity = Vector3.zero;
    12.    rigidbody.angularVelocty = Vector3.zero;
    13.    transform.position = startPos; // if you used localPosition then use it also here
    14.    transform.rotation = startRot; // if you used localRotation then use it also here
    15.  
    16.    /* TODO - all others stuff you need */
    17.  
    18. } // resetMe
    19.  
     
    TBulbuc and sahniik like this.
  3. galent

    galent

    Joined:
    Jan 7, 2008
    Posts:
    1,078
    I haven't tried:
    rigidbody.angularVelocty = Vector3.zero;

    I'll add that in and see if that helps! I can't seem to clear the results of physical collision, and I'm not sure why it's this hard?! Based on watching the debug tab I can't tell if it's the physical leftovers in the rigidbody or the constantForce not clearing, or both (or something else). Perhaps the angularVelocty is the piece I've missed. I'll give it a try and see what happens.

    Thanks,

    Galen
     
  4. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Well, if you're doing this OnCollisionEnter, it might still retain some velocity from what happens after you reset it.

    Try waiting for a frame (yield;) and then resetting everything then.
     
  5. galent

    galent

    Joined:
    Jan 7, 2008
    Posts:
    1,078
    Thanks all for your help!

    Thought I'd post an update, I did get it fixed, the key (in case someone else has this problem in the future) was resetting both elements of rigidbody:

    Code (csharp):
    1. rigidbody.velocity = Vector3.zero;
    2. rigidbody.angularVelocty = Vector3.zero;
    it turns out that the angularVelocty was being held even after all the other things were cleared. It also appears that the constantForce needs to be reset independently (if you use one), otherwise it continues to apply previously set force once the object is active again (or in my case it worked like a force multiplier, I'd add some force, disable the object, add the same force again which doubled the force, and so on).

    Hope this helps,

    Galen
     
  6. Apache

    Apache

    Joined:
    Sep 26, 2008
    Posts:
    134
    You're welcome !!!! ;-)
     
  7. schplurg

    schplurg

    Joined:
    Mar 21, 2009
    Posts:
    208
    When using the following code:
    Code (csharp):
    1.  
    2. rigidbody.velocity = Vector3.zero;
    3. rigidbody.angularVelocty = Vector3.zero;
    I get the following error:

    My object resets position, but if it resets while still moving the movement is carried over, of course.

    I hate to post something that probably has such a simple answer, but that's Unity I guess. I greatly appreciate any help.
     
  8. galent

    galent

    Joined:
    Jan 7, 2008
    Posts:
    1,078
    Well the code sample has a spelling error (if that's the code your using

    angularVelocty should be angularVelocity (an error from the example posted above).

    If that's not it, post here and I'll have another look (it is working in my code.

    Thanks,

    Galen
     
  9. schplurg

    schplurg

    Joined:
    Mar 21, 2009
    Posts:
    208
    Oh
    my
    Gawd....

    I copy pasted it from the above example. Funny...it works when spelled correctly. I will now crawl into a hole and die...after I thank you.

    :?