Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Pryzum · Nov 29, 2018 at 10:37 AM · movementrigidbodygravitytreecylinder

How to program gravity around a cylinder? (Vine growing up a tree)

With the player as the Vine growing up a tree, and other moving Rigidbodies (enemies/obstacles) along the tree, how would I go about programming gravity around the tree? Would I need unique moving gravity vectors for each Rigidbody?

Note: Need the game objects to interact like rigid bodies, so forced transform would not work,I know that this code is simple with a sphere, since there is a single, stationary center point, but how could this be done for movement around a cylinder? Would it require a moving gravity vector for each moving RigidBody along the cylinder?

Note: The player and other NPCs all need to be able to move around this cylinder, and be able to interact with each other like normal rigid bodies (no forced transform).

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Nov 29, 2018 at 11:48 PM

It works almost the same. However instead of using a single point as the center of gravity you just need to project the objects position onto the cylinders axis. To ger the point of gravity for an object you could just use Vector3.Project. However to get the actual direction of gravity you would then need to subract the object position from the specific center of gravity for this object. This can be achieved by just using the new Vector3.ProjectOnPlane which basically does this step already for you,


So for a sphere you would do something like:

 Vector3 grav = sphereCenter - objPos;

For a cylinder you would do:

 Vector3 grav = Vector3.ProjectOnPlane(cylinderCenter - objPos, cylinderAxis);

Here "cylinderCenter" can be any point that lies on the center axis of the cylinder and "cylinderAxis" is simply the direction vector / axis of the cylinder


Note that this of course only works for the "side" of a cylinder. So we assume an infinitely long cylinder. If you also want to be able to walk on the "base" disks of a finite cylinder, that's another story. However it's difficult to get a smooth transition between the gravity vector on the side surface of the cylinder and the top / bottom surface. It's also hard to tell when to switch between those. If this is needed you would probably do something like this:


Here we have to be more precise where our cylinderCenter is located. For this solution the center need to be the actual center. So the center point need to be the same distance from the top and bottom surface. Now we would do this:

 // given
 Vector3 cylinderCenter;
 Vector3 cylinderAxis; // need to be normalized
 float cylinderHalfLength;
 float cylinderRadius;
 
 // calculate gravity
 Vector3 v = cylinderCenter - objPos;
 float d = Vector3.Dot(v, cylinderAxis);
 Vector3 grav = v - cylinderAxis * d;

 if (Mathf.Abs(d) > cylinderHalfLength && grav.sqrMagnitude < cylinderRadius*cylinderRadius)
 {
     grav = -Mathf.Sign(d) * cylinderAxis;
 }

The first 3 lines of the calculation essentially is the same what "ProjectOnPlane" does. However we get some of the intermediate values which we need to determine when an object is beyond the end of the cylinder and has entered the radius of the cylinder (so he's essentially on top / or bottom of the cylinder). Note that this implementation allows an object to easily leave the curved side of the cylinder and land on the top / bottom side. However the reverse is pretty tricky. The player would need to jump at the right moment off the base disk in order to land on the side. It's possible to reverse this so it's easy to get from the top / bottom disk surface onto the curved side surface but in this case it would be tricky to get back onto the top / bottom surface. For this you just would do:

 if (Mathf.Abs(d) > cylinderHalfLength)
 {
     grav = -Mathf.Sign(d) * cylinderAxis;
 }

In this case when the player goes beyond the cylinder length gravity would switch immediately to the top / bottom surface and push him back.


If you have a different case that isn't covered by my answer, you need to be more specific about your requirements and about your desired behaviour.

Comment
Add comment · 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

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

174 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Mimic Gravity Movement Via Setting Velocity 0 Answers

Player object falling slowly when holding input 2 Answers

Cylinders running downhill. Gravity is not enough. 3 Answers

Rigidbody--Addforce on a Spherical Platform(A Globe) 2 Answers

Roll-A-Ball Movement (Without Endless Run) [C#] 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