Search Unity

Zero Gravity Movement

Discussion in 'Scripting' started by Galactic_CakeYT, Oct 9, 2020.

  1. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    65
    Currently I am making a game where my character moves around in a Space Station. I don't know how to make Zero Gravity movement. If you ever watched a video of an astronaut moving in space, you will see them using their legs and arms. In my games I want to be able to use my legs. I would kick the wall behind me, which would result in me moving forward. I am not sure how to do this though. I am currently using a Non-Rigidbody Character Controller.

    I was considering to learn Raycasting, I would shoot a ray from my legs and see if it hits a wall. If the distance between me and the wall is not exceeding my max distance, then push forward.

    Is this a good idea, or should I do something else? Also here is my code if you need it. I'll get rid of the Gravity things later, just followed a Brackeys video to make the Character Controller.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     [SerializeField] CharacterController controller;
    6.  
    7.     [SerializeField] Transform groundCheck = default;
    8.     const float groundCheckRadius = 0.25f;
    9.     [SerializeField] LayerMask groundMask = default;
    10.     bool isGround;
    11.  
    12.     const float speed = 10f;
    13.     const float gravity = -9.81f * 2;
    14.  
    15.     Vector3 velocity;
    16.  
    17.     void Awake()
    18.     {
    19.         controller = gameObject.GetComponent<CharacterController>();
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         CheckGround();
    25.         Movement();
    26.         Gravity();
    27.     }
    28.  
    29.     void CheckGround()
    30.     {
    31.         isGround = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundMask);
    32.  
    33.         if (isGround && velocity.y < 0)
    34.         {
    35.             velocity.y = 0;
    36.         }
    37.     }
    38.  
    39.     void Movement()
    40.     {
    41.         float x = Input.GetAxis("Horizontal");
    42.         float z = Input.GetAxis("Vertical");
    43.  
    44.         Vector3 move = (transform.right * x + transform.forward * z) * speed * Time.deltaTime;
    45.  
    46.         controller.Move(move);
    47.     }
    48.  
    49.     void Gravity()
    50.     {
    51.         velocity.y += gravity * Time.deltaTime;
    52.  
    53.         controller.Move(velocity * Time.deltaTime);
    54.     }
    55.  
     
  2. Meishin

    Meishin

    Joined:
    Apr 12, 2019
    Posts:
    26
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    The movement itself should be pretty easy to realise using forces and default physics (ie rigidbodies). In zero gravity you wont have to account for gravity, basically no friction, or anything else. You just apply a force and fly as long as you touch something to stop you, then decide what to do from there on. It all depends on how realistic it has to be, but i would imagine the animation part to be hardest, as in playing the correct animation for the kind of movement and direction of movement you attempt to apply. For that inverse kinematics could be useful.
     
  4. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    65
  5. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    65
    I don't want to stop my movement by touching something, I want to move by using pushing my self off something. Like Newton's Third Law said every action has an opposite and equal reaction. Basically that.
     
  6. Meishin

    Meishin

    Joined:
    Apr 12, 2019
    Posts:
    26
    @Galactic_CakeYT i havnt checked it myself but this tuto seems fine ! Also make sure to check SphereCastAll & NonAlloc in the same thread, they can be quite handy too :)
     
  7. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    I think you misunderstood me. That's exactly why i said "use actual physics", because what you need is physics. Also, i did not mean that you should stop every time you touch something. I meant it in that you can only stop by touching something. Or change your direction, or speed for that matter. That is, unless you implement a jetpack.
     
  8. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    65
    Thanks :D
     
  9. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    65
    Oh okay that makes more sense.
     
  10. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    65
    Another question. If SphereCast is almost the same as a RayCast, but instead a Sphere is at the end. Then why is Sphere Cast so common? Wouldn't using a Raycast be more performance efficient?
     
  11. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Raycasts are infinitely small lines. Sometimes you need to check if something with a specific size collides with another object in some direction. In that case a SphereCast or BoxCast can be helpful. If you just want to check for objects in some very specific direction, use a simple RayCast. And yes, of course SphereCasts are a bit more expensive than RayCasts, but unless you do thousands of those that should not result in any tangible performance loss. As a rule of thumb which you will hear often on this forum, only worry about performance optimizations after you notice an actual problem.
     
  12. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    65
    Okay, thanks
     
  13. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Have you actually done basic character movement and animations?

    Because what are you trying to do, is much more complex. Not only you need take considiration of position to obstacles, but the orientation of the astronauts. And then apply rather complex mechanics for animations.

    As mentioned earlier few post above, IK may be the way to go.

    Further tho, yet much more advance topic, but probably giving cool effects, would be using mechanim.
     
  14. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    65
    Yes, I have done the basic character movement. Animations I was going to start a bit later, I just first wanted to get the basic Zero Gravity Movement, then later I can tweak it. I do understand this is an advance topic, that is why I am trying to learn whatever I really need to. If I come up with problems then I ask for help. After this game, I am going to start learning more about the Physics and Raycasting. Things like those.