- Home /
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 game
I already see this but my planets is a cube, thats the problem!
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.
Error: Assets/Gravity.cs(1,12): error CS8025: Parsing error And i need to make what with the script ? (1/2 noob)
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. :)
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);
}
}
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