Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Is it possible to move a Rigidbody both with Physics and Transform?

Discussion in 'Scripting' started by Jason210, Apr 6, 2013.

  1. Jason210

    Jason210

    Joined:
    Oct 14, 2012
    Posts:
    127
    Is it possible to move a Rigidbody both with Physics and Transform?

    I have a 1st person controller that is based on Physics and it works just fine until it enters a vehicle. What I wanted to do was have the controller GameObjected be parented to the vehicle on entry. This is done by attaching this script to the vehicle:

    void OnCollisionEnter (Collision other) {
    if(other.gameObject.tag == "Player") {
    other.transform.parent = transform;
    }

    The problem is making the player a child doesn't work. The vehicle slides along and the player more less remains stationary, until he eventual falls off the vehicle, as if there is no friction to force the player along.

    Any ideas?
     
  2. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    676
    Rigidbody movement is conflicting with transform movement. Turn his rigidbody component off when he becomes a child.

    Code (csharp):
    1.  
    2. void OnCollisionEnter (Collision other) {
    3.  
    4.           if(other.gameObject.tag == "Player") {
    5.  
    6.                      other.transform.parent = transform;
    7.                      other.GetComponent<Rigidbody>().enabled = false;
    8.  
    9.           }
    10.  
    11. }
    12.  
    Just remember to turn it back on when you're ready to give manual control to the player.
     
unityunity