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

Question Make my player move with the rotating spherical world

Discussion in 'Physics' started by parapsychic, Sep 29, 2020.

  1. parapsychic

    parapsychic

    Joined:
    Feb 15, 2016
    Posts:
    11
    I have made a spherical world (a planet). I've done some scripting to make the player go around the world. I've coded it to "simulate" a bit of gravity so that objects are attracted to the centre of the world and stay upright. I tried taking it a bit higher than I originally planned with the planet rotating about its own axis. But, the problem is that my player(a capsule) won't move (rotate) with the planet. That is, when standing still, I want the capsule to have some friction with the ground. I tried adding a Physics material, but no amount of friction is helping. I suspect if it has to do with the capsule touching the world on just one point. So, I tried to replace it with a cube. But, I'm still not getting that friction going. What should I try to make it so that my player has "no relative movement with the world when standing still"?
     
  2. parapsychic

    parapsychic

    Joined:
    Feb 15, 2016
    Posts:
    11
    For anyone looking on how to do it, here's how I solved it:
    I used OverlapSphere (you could use onCollision methods) to check if my player is touching with the world. If it is, then I set the player's parent to the Planet. When not touching the world, I set its parent to null.
    Code (CSharp):
    1. if(Physic.OverlapSphere(transform, 1f, planetLayerMask){
    2. transform.SetParent(planetTransform);
    3. {
    4. else transform.SetParent(null);
    Obviously, this is not the best way to do this with all the ifs and elses. But, I'm just starting out, so I guess it's okay (?).
    Also, you could modify the code a bit to make an orbit around the planet. Instead of using OverlapSphere to detect a collision, check if your player is in the "gravity" distance from the planet. For anyone who is new to Vectors, in school, we used to subtract each component and then get its magnitude. We'll be doing the same here too. Note that this will return the distance from the center (in my case origin) of the planet. So, it'll be
    dir =transform.position - planetTransform.position 
    You can easily get the magnitude of dir by
    dir.magnitude
    .
    Then
    Code (CSharp):
    1. if( dir.magnitude > gravityExitDistance ){
    2. transform.SetParent(parentTransform);
    3. }
    4. else transform.SetParent(null);
    Again, this is not the most efficient way, "but it works". If anyone can correct me, you are welcome. Because it'll be of huge help with my game.