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): using UnityEngine; public class PlayerMovement : MonoBehaviour { [SerializeField] CharacterController controller; [SerializeField] Transform groundCheck = default; const float groundCheckRadius = 0.25f; [SerializeField] LayerMask groundMask = default; bool isGround; const float speed = 10f; const float gravity = -9.81f * 2; Vector3 velocity; void Awake() { controller = gameObject.GetComponent<CharacterController>(); } void Update() { CheckGround(); Movement(); Gravity(); } void CheckGround() { isGround = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundMask); if (isGround && velocity.y < 0) { velocity.y = 0; } } void Movement() { float x = Input.GetAxis("Horizontal"); float z = Input.GetAxis("Vertical"); Vector3 move = (transform.right * x + transform.forward * z) * speed * Time.deltaTime; controller.Move(move); } void Gravity() { velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); }
Hi @Galactic_CakeYT, You can idd detect walls with SphereCast (put your walls in a separate layer from your character, and apply SphereCast only on walls layer). For the force technicality (realistic movement) you can check https://answers.unity.com/questions/175325/realisticno-gravity-spaceship-movement.html
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.
Thanks. But can you recommend me somewhere, where I can learn about SphereCasts because I am still kind of new to the physics of Unity.
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.
@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
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.
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?
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.
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.
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.