Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by xSpectrum · Dec 08, 2012 at 09:35 PM · physicsraycastgravitynormals

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave-Carlile · Dec 09, 2012 at 04:15 PM 2
Share

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.

avatar image lil_billy · Dec 09, 2012 at 04:32 PM 2
Share

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.

avatar image AlucardJay · Dec 09, 2012 at 08:46 PM 0
Share

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 =]

avatar image xSpectrum · Dec 11, 2012 at 05:24 AM 0
Share

@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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xSpectrum · Dec 11, 2012 at 05:27 AM 0
Share

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.

avatar image lil_billy · Dec 11, 2012 at 06:04 AM 0
Share

......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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges