The Realm of Real

..rjproz's blog..

Physics Forces – Circular Gravity , AntiGravity , Projectile and Buoyancy

Physics simulations are one of the most aspect of 3D games. Here’s a collection of 4 Physics forces simulation scripts which can simulate Circular Gravity (Universal law of gravitation),Circular Anti-Gravity (or Repulsive forces), Buoyancy and Projectile trajectory.

See Web player demo here for all forces.

Documentation :

Gravity :

Drag the script “PlanetaryGravity.cs” on the object you want to execute gravity.
There ate various parameters :-


–  Range Of gravity : To limit the range of force.
–  Mass : Mass of object. It will override the default mass of attached rigidbody (if rigidbody is attached)
– Layer Mask : It gives you the option on which layers the force will act.
– Gravity Type : It has two choices i.e Gravity and Anti-Gravity.
– G : It is Gravitational Constant which appears in Newtons law of gravitation. You can change it if you want.

You can also use this to simulate Magnetic or Electric Forces.

Buoyancy :

It consist two things, one Fluid and other Objects.

The Object in which you want to simulate fluid physics, must have tag as “Fluid” as shown above. Drag Buoyancy Fluid script on it. Also it’s collider must be marked as isTrigger.

Property “Density Of Fluid” is very important during simulation.

The Object in which you want to simulate Buoyancy, drag “Buoyancy Object” script on it. The object must have rigidbody attached with. The script automatically calculates the Object’s volume. You can change its mass from rigidbody to see different effects. Note that the density of Object must be less than that of fluid to make the object float above the fluid.

Projectile :

This is totally scripting package. If you want to target a projectile to a particular position with a particular velocity than it will provide you the euler angles at which the projectile must be inclined to accomplish that.

It has two functions :

public static bool GetProjectileRotation(Vector3 SourceLocation,Vector3 Destination,float maximumProjectileSpeed,bool moreThan45,out Vector3 Eulers)

So to use it,

Vector3 eulers;
   
  isInsideOfRange=  Projectile.GetProjectileRotation(transform.position,loc,MaxSpeed,loadAngleMoreThan45,out eulers);
  if(!isInsideOfRange)
  {
   return;
  }
  GameObject projectile = Instantiate(projectilePrefab,transform.position,Quaternion.identity) as GameObject;
  projectile.transform.eulerAngles = eulers;
  //print(eulers);
  
  projectile.rigidbody.velocity = projectile.transform.forward * MaxSpeed;

The function will return false, if destination is out of range.

Second function is similar but calculate information about the shot like Time of flight and Maximum height reached.

public static bool GetProjectileRotationWithInfo(Vector3 SourceLocation,Vector3 Destination,float maximumProjectileSpeed,bool moreThan45,out ProjectileInfo projectileInfo)

So, to use it :-

ProjectileInfo projectileInfo;
   
  isInsideOfRange=  Projectile.GetProjectileRotationWithInfo(transform.position,loc,MaxSpeed,loadAngleMoreThan45,out projectileInfo);
  if(!isInsideOfRange)
  {
   return;
  }
  GameObject projectile = Instantiate(projectilePrefab,transform.position,Quaternion.identity) as GameObject;
  projectile.transform.eulerAngles = projectileInfo.EulerAngles;
  //print(eulers);
  
  projectile.rigidbody.velocity = projectile.transform.forward * MaxSpeed;
  
  info = "";
  info+="Time Of Flight : "+projectileInfo.FlightTime+" sn";
  info+="Maximum Height Reached : "+projectileInfo.MaximumHeightReached +" m";

Asset Store Link

For any queries or suggestions, drop a mail at rjproz@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *