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. Dismiss Notice

teleport a rigidbody

Discussion in 'Scripting' started by bumba, Sep 23, 2010.

Thread Status:
Not open for further replies.
  1. bumba

    bumba

    Joined:
    Oct 10, 2008
    Posts:
    358
    Hi,

    i want to releport my rigidbody. I will add a Force and if the rigidbody touches the trigger i will move the rigidbody to another position and the force should continue. The problem is the rigidbody is going crazy after moving. This is what im doing:

    Code (csharp):
    1.  
    2. function OnTriggerEnter(other : Collider) {
    3. //this is my rigidbody gameobject
    4. other.gameObject.transform.position = exit.transform.position;
    5. }
    6.  
     
  2. jingato

    jingato

    Joined:
    Jun 23, 2010
    Posts:
    299
    Looks fine to me. Maybe something else is causing the issue.
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you can sleep it, then move the rigidbody (through rigidbody.position, not transform position) and wake it again. that should work I think.
     
  4. bumba

    bumba

    Joined:
    Oct 10, 2008
    Posts:
    358
    i tried it but the behaviour of the rigidbody is not always the same. Sometimes it will drop down like a stone and sometimes it will be faster as before
     
  5. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    Have you tried something like this:

    1) store rigidbody.velocity
    2) store rigidbody.angularvelocity
    3) sleep rigidbody
    4) deactivate gameObject
    5) move gameObject
    6) activate gameObject
    7) forcibly reset rigidbody.velocity/angularvelocity
    8) wake rigidbody

    ----

    Maybe swapping 7/8 if the first attempt doesn't work? You prooobably don't need to deactivate the gameObject, but you said simply sleeping/waking the rigidbody wasn't working - so it's worth a shot.
     
  6. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    the way to move it would be:
    1. record the velocity. (rigidbody.velocity)
    2. set the rigidbody to kinematic (isKinematic = true)
    3. move the transform to the new location
    4. set isKinematic = false
    5. recompute the new velocity direction (that is based on the in and out portals orientations and the initial rigidbody velocity)
    6. set rigidbody.velocity = to the transformed initial rigid body velocity.

    EDIT: I just saw that Kyle posted 3 min. before me.
    I did not post to correct his approach. In fact his approach seems good to me. If the portals are oriented in the same way, you don't have to worry about transforming the velocity vector.
     
  7. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    No offense taken :D
    I don't work with rigidibodies for the most part; was just a stab-in-the-dark attempt on my part. Didn't even think to take the orientations of the portals into consideration :)

    Whatever gets the OP up and running is, as they say, for the win.
     
  8. bumba

    bumba

    Joined:
    Oct 10, 2008
    Posts:
    358
    @ivkoni @KyleStaves i tried your approaches but the problem is the same. The behavior is not always the same. Sometime the rigidbody goes crazy. Sometimes it drop downs.... :-(
     
    Nodragem likes this.
  9. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    I'd check to make sure you don't have anything solid in the destination area that the object could intersect with. If you do, it'll cause the crazy physics you're experiencing.
     
    igorskugar likes this.
  10. Tony-Lovell

    Tony-Lovell

    Joined:
    Jul 14, 2014
    Posts:
    127
    It's absurd that Unity makes these things so difficult.
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    You can set the Rigidbody.position to make it instantly move to that position.

    TBH what is absurd is necro-posting to a 12 year old thread to complain about such a simple thing.

    Please don't do this, there's got to be a much more productive use of your time here. :)
     
Thread Status:
Not open for further replies.