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 StuwuStudio · Feb 09, 2015 at 09:57 PM · rotationgravitycube

Cube gravity - player rotation around a planets

sorry i speak french :|

How to do that: I want to make a cubic planets game with gravity. I want to the player rotate when they are in top or on the axi of a face of the block. if a player don't touch the face, he will affected by gravity for touch-it. Note: if the player are far of the planets, he won't be affected by gravity. Block building gamealt text

shematicsccp2.png (225.6 kB)
Comment
Add comment · Show 2
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 melkorinos · Feb 16, 2015 at 08:28 PM 0
Share

Take a look at this tutorial

avatar image StuwuStudio · Feb 16, 2015 at 08:38 PM 0
Share

I already see this but my planets is a cube, thats the problem!

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by VesuvianPrime · Feb 09, 2015 at 10:18 PM

Firstly, we need a method for calculating the gravitational force between two bodies. The following is a loose implementation of Newtons law of Universal Gravitation, but you could use linear, quadratic, etc falloff:

 public const float G = 1000.0f; 
 
 public static float UniversalGravitation(Rigidbody a, Rigidbody b)
 {
     return G * (a.mass * b.mass) / Vector3.Distance(a.transform.position, b.transform.position);
 }

Now we need to figure out the direction of the gravity (I'll do this in 3D because you didn't specify 2D or 3D):

 private static Vector3[] s_GravityDirections = {Vector3.up, Vector3.down, Vector3.left, Vector3.right, Vector3.forward, Vector3.back};
 
 public static Vector3 CubeGravityForce(Rigidbody planet, Rigidbody other)
 {
     float gravity = UniversalGravitation(planet, other);
 
     float bestDot = -1.0f;
     Vector3 direction = Vector3.zero;
 
     for (int index=0; index < s_GravityDirections.Length; index++)
     {
         Vector3 gravityDirection = planet.transform.TransformVector(GravityDirections[index]);
         Vector3 directionToOther = other.transform.position - planet.transform.position;
 
         float dot = Vector3.Dot(gravityDirection.normalized, directionToOther.normalized);
 
         if (dot > bestDot)
         {
             direction = gravityDirection;
             bestDot = dot; 
         }
     }
 
     return gravity * direction.normalized * -1.0f;
 }

And I think that should work.

Edit:

For rotation you can simply calculate the direction of the force in the same way as gravity direction, and pass that as your "up" into Quaternion.LookRotation.

To cutoff the gravity after a certain distance you can simply check the distance and return 0.0f.

Comment
Add comment · Show 22 · 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 StuwuStudio · Feb 10, 2015 at 12:29 AM 0
Share

Error: Assets/Gravity.cs(1,12): error CS8025: Parsing error And i need to make what with the script ? (1/2 noob)

avatar image StuwuStudio · Feb 10, 2015 at 12:46 AM 0
Share

Yes, is it 3D. I need to put the script to what item? And what is planet? The gravitational object probably... It a C# or Javascript, probably C#... It a little bite hard to understand ;P But thank for that. :)

avatar image VesuvianPrime · Feb 10, 2015 at 01:04 AM 1
Share

I have tested, corrected and rewritten the solution as a $$anonymous$$onoBehaviour (C#):

 using UnityEngine;
 using System.Collections;
 
 public class GravityTest : $$anonymous$$onoBehaviour
 {
     public float gravity = 100.0f;
     public float cutoffDistance = 10.0f;
     public Rigidbody planet;
     public Rigidbody player;
 
     private Vector3[] m_GravityDirections =
     {
         Vector3.up,
         Vector3.down,
         Vector3.left,
         Vector3.right,
         Vector3.forward,
         Vector3.back
     };
     
     private float UniversalGravitation(Rigidbody a, Rigidbody b)
     {
         float distance = Vector3.Distance(a.transform.position, b.transform.position);
         if (distance >= cutoffDistance)
             return 0.0f;
 
         return gravity * (a.mass * b.mass) / distance;
     }
 
     private Vector3 CubeGravityDirection(Rigidbody planet, Rigidbody other)
     {
         float bestDot = -1.0f;
         Vector3 direction = Vector3.zero;
         
         for (int index=0; index < m_GravityDirections.Length; index++)
         {
             Vector3 gravityDirection = planet.transform.TransformVector(m_GravityDirections[index]).normalized;
             Vector3 directionToOther = (other.transform.position - planet.transform.position).normalized;
             
             float dot = Vector3.Dot(gravityDirection, directionToOther);
             
             if (dot > bestDot)
             {
                 direction = gravityDirection;
                 bestDot = dot;
             }
         }
         
         return direction;
     }
 
     private Vector3 CubeGravityForce(Rigidbody planet, Rigidbody other)
     {
         float gravity = UniversalGravitation(planet, other);
         Vector3 direction = CubeGravityDirection(planet, other);
 
         return gravity * direction * -1.0f;
     }
 
     public void FixedUpdate()
     {
         Vector3 gravityForce = CubeGravityForce(planet, player);
         player.AddForce(gravityForce);
 
         Vector3 up = CubeGravityDirection(planet, player);
         player.transform.rotation = Quaternion.LookRotation(player.transform.forward, up);
     }
 }

avatar image StuwuStudio · Feb 10, 2015 at 12:54 PM 0
Share

Work! Thank!

avatar image StuwuStudio · Feb 10, 2015 at 10:30 PM 0
Share

Problem: [Can't add component 'GravityForce (name file)' because it doesn't exist. Check to see if the file name and class name match.] and i need to put the script to what item, and rename what? plz help

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Cubic planets with gravity and player rotation. 0 Answers

How can i rotate tyhorugh local axis? 1 Answer

Rotation not working properly(3D) 1 Answer

How to make a character walk on a 3D cube planet and a long 3D cube planet using gravity? 2 Answers

Is there a way to put images on each side of a cube,so that only clickable areas of the cube would be those images ? 0 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