- Home /
Applying Normal force from ground to player
I'm making a 2-d platformer game. I want to have a lot of small planets that the player can jump between, so I conjured up the following script which calculates the gravitational force of the planet on the player.
var player:Transform;
var distance_squared : float;
var force : float;
var force_vector : Vector3;
var force_direction:Vector3;
var gravitational_constant : float;
function Update ()
{
gravitational_constant = 1;
distance_squared = (transform.position - player.position).sqrMagnitude;
var mass1 = 1000;
var mass2 = 1;
force = gravitational_constant * ((mass1 * mass2) / distance_squared);
force_direction = (transform.position - player.position).normalized;
force_vector = force_direction * force;
rigidbody.mass = mass1;
player.rigidbody.mass = mass2;
player.rigidbody.AddForce(force_vector);
}
The problem is that the "planet" that this is assigned to is just a plane with a circular Texture applied to it, and therefore, the player just goes right through the "planet". To (try to) fix this, I assigned the following code to the player:
var Mars : Transform;
var hit : RaycastHit;
var NormalTimesGravity : Vector3;
function Update ()
{
if (Physics.Raycast (transform.position,Mars.position-transform.position,hit,1))
{
NormalTimesGravity=(hit.normal*Mars.GetComponent(Gravity).force);
Debug.DrawRay(transform.position,Mars.position-transform.position);
rigidbody.AddForce(NormalTimesGravity);
}
For whatever reason, this code only seems to slow down the player and not stop him completely. My thought was that the "planet"'s surface needed to push the player in the normal direction of the player with the magnitude of the gravitational force the player has. That is how physics works in real life, is it not? For example, a 100 gram object with a gravitational acceleration of 9.81 (Earth's gravitational pull), The surface of Earth is pushing the object up at 981 N in the normal direction. So why therefore is this script not stopping the object completely.
P.S. if I do something like `if (Physics.Raycast (transform.position,Mars.position-transform.position,hit,3))` (increase the number in the Raycast), the object will not go through the planet (it stops before it hits the surface), but it also just bounces along the 3 meters out from the planet's surface.
For the record, you didn't conjure up that script entirely on your own. $$anonymous$$uch of it was provided by my answer to your previous question: http://answers.unity3d.com/questions/359759/how-to-program-gravity-for-planets.html
An answer you didn't have the courtesy to accept, and now claim the code as your own.
lol wow, damnit and i just answered this one, man i hate people who dont rate, kills my acceptance rating just to make these answers, would rather have it killed if my answer was crap then someone not care to acknowledge it.
though im always in awe of those generous people who actually go through the effort to literally write code for people. So not worth the time and trouble.
That's just the way the banana splits! I gave up worrying about the acceptance rate after 2 months, karma is really all you should worry about. So I shall upvote you both Dave and lil_billy, thanks for supporting the 'site. btw Dave, I actually have your answer bookmarked, great work =]
@Dave Carlile I didn't mean to imply that I did. I realize it was a poor choice of words, but have now accepted the other guy's answer on the other thread.
Answer by lil_billy · Dec 09, 2012 at 03:10 PM
nooooo nothing good comes when trying to fight the physics engine with more physics and it sounds like you are over complicating the problem. in this case you need to use colliders better. So you need to have it so that it is not applying the force when the player is colliding with a collider of the ground ( this will remove some unsightly jitter)
now for your actual problem
im not sure how you are using a plane to emulate a spherical planet flipping props if its to the scale and detail as i imagine. But since i can only guess im gonna say strap a box collider that encompasses the bounds of the plane and that should fix your problem. so long as the player remains on that plain.
I love the rigid body system, its VERY responsive with colliders. But it truly is best to use rigidbody commands as little as possible both for efficiency reasons and just to avoid over complicating things. Fighting physics with physics is just all around bad.
Colliders would not work right. I've tried it, and since it's a 2-d world with spherical objects, it never works. The player just skids right off of the planet most of the time. I'd like more insight as to if there is something I could change in the script, or if Unity is the one at fault here, and not the cod itself.
......no it your code. or i should say the code you mig shifted from someone else and dont quite understand.
just because you have a script doesnt mean its gonna work, you have to implement in regard to the rest of the SYSTE$$anonymous$$ in other words UNITY. You have to use that script in conjunction with rigidbodies and colliders
Dave didnt write that script to make your game for you, he gave you a starting point. You need to craft it to meet the special circumstances present in your game(thats what scripting is all about).
Playing with the built in values of colliders and rigidbodies will solve many of the problems you are describing however others would need to be fine tuned.
Id recommend perhaps looking up some basics on colliders and rigidbodies to help you with this, rigidbodies govern motion where as a collider governs collision, so if you are sliding off of a plane does that mean collision is broken or that you are still moving? A: means your still moving because if collision was broken youd go straight through. From there you could try detecting collision with and OnCollisionEnter function to try and halt your rigidbody.
you can set friction values weight and so on and so forward
now i can come up with psuedo code all day long but I dont understand your game mechanic. you say 2D plat former with planes but then say spherical planet gravitation. Ive played some games like this but i wasnt jumping onto a plane that was a planet, so that perspective has got me confused to the point where i cant begin to approach a solution on how you should go about making that game.
Your answer
Follow this Question
Related Questions
Calculate where an object is going to land 0 Answers
false gravity on a cube planet 6 Answers
[C#] Raycast based physics and clipping 0 Answers
Do Mesh Normals Influence Physics Behavior 1 Answer
Raycasting problem 2 Answers