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

How to Move Kinematic Rigidbody along a trigger object vector

Discussion in 'Scripting' started by Vossy, Sep 6, 2014.

  1. Vossy

    Vossy

    Joined:
    Feb 18, 2014
    Posts:
    60
    So i'm trying to achieve the same effect as for example in Tony Hawks games, where the player could grind along the ledges and rails. The general idea is that in Unity, there is a trigger box. Once the rigidbody hit the trigger, i can grab the trigger objects transform( position, rotation ) and set the rigidbody.isKinematic = true.
    Now i would need to smoothly move the player kinematic rigidbody to that position of the trigger and make it move along with the trigger vector.
    Now what happens is: the object hits the trigger and get stuck(no movement at all).
    I've tried to use rigidbody.MovePosition like this:

    Code (CSharp):
    1.  
    2. void OnTriggerEnter( Collider trig )
    3. {
    4.       myTransform = this.transform;
    5. }
    6.  
    7. //Setting the player kinematic rigidbody object to the position of trigger in FixedUpdate()
    8. Vector3 delta = new Vector3(myTransform.position.x, myTransform.position.y, myTransform.position.z) - player.position;
    9.  
    10. delta = delta.normalized;
    11.  
    12. rigidbody.MovePosition( rigidbody.position + delta * ( 3 * Time.deltaTime) );
    13.  
     
  2. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    I'm not sure it works while isKinematic = true.
     
  3. Conan_cs

    Conan_cs

    Joined:
    Apr 5, 2014
    Posts:
    16
    after you set the gameObjects rigidbody.isKinematic = true physic will no longer affect your gameobject. You can translate your GameObject with transform.Translate.
    You would achieve the effect by freezing the rotation and then moving the Object with rigidbody.MovePosition.
     
  4. Vossy

    Vossy

    Joined:
    Feb 18, 2014
    Posts:
    60
    Yeah movePosition does work with Kinematic Rigidbodies. But if you don't know then why to answer..

    So that was the question, how excacly calculate the movement to the positon(and angles)?
     
  5. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    ...