Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make a rigidbody inherit another rigidbodies velocity

Discussion in 'Scripting' started by Mike L, Mar 30, 2013.

  1. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    how can i make my player inherit the velocity of the thing it is standing on. I have my character set up with a rigidbody, and i move it by changing the velocity variable, and it works pretty well most of the time. I know im not really supposed to do that, but I dont know another way to do it. any way, i have raycasts determine if the player is on the ground or not, so finding the velocity of the object the player is standing on is not a problem, the problem is how to i make sure that every frame the player has the correct velocity, because if i simply add the movement velocity, the player accelerates because its taking the pervious frames velocity, and adding to it every frame, which obviously doesnt work.
     
  2. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    can i please get some help?
     
  3. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    I'm not really sure what you want to do? I think you are basically wanting to replicate the theory of relativity? like if P(player) is moving at 10 meters a second and is on a platform moving at 20ms then relative to the world P is moving at 30 meters a second? You could do this by making the player a child of the floor, since you say you ray casting sting you could use that.

    Code (csharp):
    1.  
    2. transform.parent = hit.transform;
    3.  
    were hit is the raycast hit of your ray.
     
  4. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    that is precisely what i am tying to do, the only problem though, when i tried that it didnt work. maybe its my code that makes it behave kinda wonky though...... (if i want the player to not have a parent I can just use transform.parent = null; right? i put that in but i didnt check to see it it worked because nothing else for it really did....)
    Code (csharp):
    1. if (Physics.Raycast(transform.position, -transform.up, out hit, 13)) {
    2.                 transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
    3.                 if (hit.distance <= 1) {
    4.                    
    5.                     if (Input.GetButton("Jump")) {
    6.                         rigidbody.AddRelativeForce(0, 10000, 0);
    7.                     }
    8.                    
    9.                     rigidbody.velocity = transform.TransformDirection(movement*moveSpeed);
    10.                 }
    11.                
    12.                 else {
    13.                     if (Input.GetButton("Jump")) {
    14.                         movement.y = 1;
    15.                     }
    16.                    
    17.                     else {
    18.                         movement.y = -9.1f;
    19.                     }
    20.                     constantForce.relativeForce = movement * rigidbody.mass;
    21.                 }
    22.             }
    thats my code for the movement right now, its not really that complicated.... not sure why it would make it behave the way it is though... (also, some more info, the game is in space, so no gravity, but i have it so the player can lock to objects, but the problem i was having was precisely the theory of relativity.... i tried doing this thing were i checked if the object had a rigidbody, and if it did, it would execute this code
    Code (csharp):
    1. rigidbody.velocity = hit.rigidbody.velocity + transform.TransformDirection(movement*moveSpeed);
    instead of this
    Code (csharp):
    1. rigidbody.velocity = transform.TransformDirection(movement*moveSpeed);
    to move the player, but it didnt work completely, i also set the players angular velocity to the objects angular velocity at the same time, and the object the player was standing on decided to start spinning around ant an ever increasing speed.....)
     
  5. Alnos

    Alnos

    Joined:
    Aug 20, 2019
    Posts:
    110
    I'm having proble with similar thing. Have you found a solution?