- Home /
How would I apply gravity to a game object and have it collide with the ground beneath it by using raycasts without using Rigidbodies?
So if I had a project with two game objects that both had BoxCollider2D components attached to them one being the Player and the other the ground how would I go about appling gravity to the Player so that it would collide with the ground below it?
I thought I could apply gravity to the Player by using the the transform.Translate function in order to force the Player to travel downwards on the Y axis. While this is happening I could shoot a raycast from the bottom of the Player's bounding box so that when it hits the ground I could use the "hit" information to make the Player stop once it reaches the ground.
But I don't know how to go about doing this? Could someone help me with trying to make this work?
Answer by logicandchaos · Apr 27, 2021 at 02:14 PM
But why? Thee is built in gravity and collision. If you don't want to use rigidbodies it means writing your own physics code. Just look at how people do it in engines that don't have physics. to make it fall using translate you would have to call translate from update or a coroutine and calculate how much to move it to keep uniform motion. You could have a coroutine called fall and exit it isGrounded=true or when the raycast = true; But what are you trying to accomplish by not using rigidbodies?
Well it's because I watched a Youtube tutorial on 2D platformers by Sebastian Lague that is highly regarding here:
In this tutorial he does not use Rigidbodies at all and creates his own physics because you can achieve more fine tuning and control over your Player's movement this way according to him.......but I have no idea whether this is true or not? A lot of people seem to think it is true and that you can achieve more control by exclusively using Raycasts.
Can I ask what your opinion on this is?
eyy, Sebastian Lague! hes the reason i got into deving in the first place.
while i have no experience with your particular problem, i noticed Sebastian left the source code in his video. maybe you could look into it and try to reverse engineer it?
I think you can achieve the same or relatively close fine tuning and control if you know the physics engine well, you can adjust all the gravity and mass and velocities.. I think you get the smoothest most uniform motion when you use a physics engine, but it maybe be heavier.. like if you are shooting a cannon, and creating the trajectory, if you use built in in physics you say AddForce(someVector); one line you are done! Now program$$anonymous$$g in without rigidbodies you need to calculate everything! Just seems like reinventing the wheel and makes me wonder why he is using unity and not just a graphics framework.. You can change everything about the unity built in physics, so I'm not sure how you get more fine control.. seems to be just about preference to me.. what I find is if you watch a video about the depompilation of the mario jump they say you fall faster than you rise. which isn't exactly physics correct, but it feels better. so to do this in unity with rigidbodies, you can make your own gravity force, which I have done in the past, or you can manipulate the gravity of the object. You go
if(rb.velocity.y<-1)
rb.gravityScale=2;
else
rb.gravityScale=1;
Answer by adityajaix · May 03, 2021 at 05:41 PM
I am beginner as well but here what I tried simple and easy way. And it works I created an empty game object, make it child of the Character. Place it a little below it. I used set position to move towards child empty game object with a speed variable. I calculated Speed variable in a fixed update if not grounded set speed = speed + rate float variable. and if grounded set speed to 0. try changing rate to make acceleration slow or fast.