public static void AddExplosionForce(this Rigidbody2D body, float explosionForce, Vector3 explosionPosition, float explosionRadius, float upliftModifier)
        {
            var dir = (body.transform.position - explosionPosition);
            float wearoff = 1 - (dir.magnitude / explosionRadius);
            Vector3 baseForce = dir.normalized * explosionForce * wearoff;
            body.AddForce(baseForce);

            float upliftWearoff = 1 - upliftModifier / explosionRadius;
            Vector3 upliftForce = Vector2.up * explosionForce * upliftWearoff;
            body.AddForce(upliftForce);
        }
    /// <summary>
    /// Function for applying a specific type of force to a Rigidbody2D (since the default functionality is incomplete)
    /// </summary>
    /// <param name="rb2d">Rigidbody2D to apply the force to</param>
    /// <param name="force">The amount of force to apply</param>
    /// <param name="mode">What type of force to apply</param>
    /// <param name="relativeTo">Should the force be applied in the rigidbody's local space, or world space?</param>
    public static void AddForce2D(this Rigidbody2D rb2d, Vector2 force, ForceMode mode = ForceMode.Force, Space relativeTo = Space.World)
    {
        ForceMode2D mode2D = ForceMode2D.Force;
        Vector2 forceApplied = force;
        switch (mode)
        {
            case ForceMode.Impulse:
                mode2D = ForceMode2D.Impulse;
                break;
            case ForceMode.Acceleration:
                forceApplied *= rb2d.mass;
                break;
            case ForceMode.VelocityChange:
                forceApplied = force * rb2d.mass / Time.fixedDeltaTime;
                break;
            case ForceMode.Force:
                //nothing special
                break;
        }

        if (relativeTo == Space.Self)
            rb2d.AddRelativeForce(forceApplied, mode2D);
        else if (relativeTo == Space.World)
            rb2d.AddForce(forceApplied, mode2D);
    }
示例#3
0
 /// <summary>
 /// Applies a force to the Rigidbody2D such that it will land, if unobstructed, at the target position.
 /// The arch [0.0f, 1.0f] determines the percent of arch to provide between the minimum and maximum arch.
 /// An arch or 0 or 1 will set the trajectory such that the entire force is being applied.
 /// Any value between 0 and 1 will have a reduced force.
 /// If target is out of range, it will still fire, but not reach the target.
 /// Use rigidbody2D.IsWithinRange(Vector2, float) to determine if the target is within range ahead of time, if destired.
 /// This only takes the Y gravity into account, and X gravity will not affect the trajectory.
 /// If there is no gravity and no ConstantForce2D is attached, a ConstantForce2D with a gravity of (0.0f, -9.8f) will be attached.
 /// </summary>
 public static bool SetTrajectory(this Rigidbody2D rigidbody2D, Vector2 target, float force, float arch = 0.5f)
 {
     arch = Mathf.Clamp(arch, 0, 1);
     var origin = rigidbody2D.position;
     float x = target.x - origin.x;
     float y = target.y - origin.y;
     float gravity = -Physics2D.gravity.y;
     if (gravity == 0)
     {
         var constantForce2D = rigidbody2D.GetComponent<ConstantForce2D>();
         if (!constantForce2D)
         {
             Debug.LogWarning("There is no gravity and " + rigidbody2D.name + " does not have a ConstantForce2D attached.  A ConstantForce2D with the default gravity of -9.8f will be added.");
             constantForce2D = rigidbody2D.gameObject.AddComponent<ConstantForce2D>();
             constantForce2D.force = new Vector2(0, DefaultGravity);
         }
         gravity = -constantForce2D.force.y;
     }
     float b = force * force - y * gravity;
     float discriminant = b * b - gravity * gravity * (x * x + y * y);
     discriminant = Mathf.Max(0, discriminant);
     float discriminantSquareRoot = Mathf.Sqrt(discriminant);
     float minTime = Mathf.Sqrt((b - discriminantSquareRoot) * 2) / Mathf.Abs(gravity);
     float maxTime = Mathf.Sqrt((b + discriminantSquareRoot) * 2) / Mathf.Abs(gravity);
     float time = (maxTime - minTime) * arch + minTime;
     float vx = x / time;
     float vy = y / time + time * gravity / 2;
     var trajectory = new Vector2(vx, vy);
     trajectory = Vector2.ClampMagnitude(trajectory, force);
     rigidbody2D.AddForce(trajectory, ForceMode2D.Impulse);
     return true;
 }
   public static void AddExplosionForce(this Rigidbody2D body, float explosionForce, Vector3 explosionPosition,
 float explosionRadius)
   {
       var dir = (body.transform.position - explosionPosition);
         var wearoff = 1 - (dir.magnitude/explosionRadius);
         body.AddForce(dir.normalized*explosionForce*wearoff);
   }
	public static void AddForce (this Rigidbody2D rigidbody2D, Vector2 force, ForceMode mode = ForceMode.Force) {

		switch(mode){
		case ForceMode.Force:
			rigidbody2D.AddForce (force);
			break;
		case ForceMode.Impulse:
			rigidbody2D.AddForce (force / Time.fixedDeltaTime);
			break;
		case ForceMode.Acceleration:
			rigidbody2D.AddForce (force * rigidbody2D.mass);
			break;
		case ForceMode.VelocityChange:
			rigidbody2D.AddForce (force * rigidbody2D.mass / Time.fixedDeltaTime);
			break;
		}
	}
示例#6
0
	public static void AddRandomForceOnY(this Rigidbody2D body, float Min, float Max, bool randDirection, ForceMode2D mode)
	{
		int direction = 1;
		if(randDirection)
		{
			direction = (Random.value > 0.5f) ? 1 : -1;
		}
		float y = Random.Range (Min, Max); 
		body.AddForce (new Vector2 (0, direction * y), mode);
	}
 public static void AddExplosionForce ( 
     this Rigidbody body
     , float explosionForce
     , Vector3 explosionRadiusCenter
     , float explosionRadius
     , Vector3 explosionOriginPoint // this is the oposite from upwardsModifier
     , ForceMode mode
 )
 {
     if (Vector3.Distance(body.transform.position, explosionRadiusCenter) <= explosionRadius) 
     {
         Vector3 force = (body.transform.position - (explosionRadiusCenter + explosionOriginPoint));
         body.AddForce(force * (explosionForce/5), mode); // 5 came from experimentation
     }
 }
示例#8
0
    public static bool moveTo(this Rigidbody rb, Vector3 to, Vector3 velocity,
	                          float speed)
    {
        Vector3 distance = to - rb.position;
        float sqrMag = distance.sqrMagnitude;
        //		Debug.Log (distance.sqrMagnitude + " " + velocity.sqrMagnitude + " " + lastSqrMag);
        if (sqrMag < lastSqrMag || lastSqrMag == 0) {
            rb.AddForce(velocity, ForceMode.VelocityChange);
            lastSqrMag = sqrMag;
            return false;
        } else {
            rb.velocity = Vector3.zero;
            lastSqrMag = 0;
        }
        return true;
    }
 public static void AddForce(this Rigidbody2D rigidbody2D, float x, float y, ForceMode mode = ForceMode.Force)
 {
     rigidbody2D.AddForce(new Vector2(x, y), mode);
 }
示例#10
0
		public static void AddHorizontalForce(this Rigidbody2D rb, float force) {
			rb.AddForce(new Vector3(force, 0, 0));
		}
示例#11
0
		public static void AddVerticalForce(this Rigidbody2D rb, float force) {
			rb.AddForce(new Vector3(0, force, 0));
		}
 public static void CapSpeed(this Rigidbody rb, float speed)
 {
     if (rb.velocity.sqrMagnitude > speed*speed) {
         rb.AddForce (-rb.velocity.normalized * (rb.velocity.magnitude - speed), ForceMode.VelocityChange);
     }
 }
 public static void AddRelativeForce(this Rigidbody2D body, Vector2 force)
 {
     body.AddForce(body.transform.TransformDirection(force));
 }