Iuvo Unity 0.5.1
This is library containing a variety of helper classes and extension methods for the Unity gane engine.
GitHub | Iuvo Unity Updates | Creator
Loading...
Searching...
No Matches
IuvoUnity.Extensions.RigidbodyExtensions Class Reference

Extension methods for Unity's Rigidbody component, providing utility methods for velocity, forces, rotation, and movement control. More...

Static Public Member Functions

static void SetVelocityX (this Rigidbody rb, float velocity)
 Sets the X component of the Rigidbody's velocity.
static void SetVelocityY (this Rigidbody rb, float velocity)
 Sets the Y component of the Rigidbody's velocity.
static void SetVelocityZ (this Rigidbody rb, float velocity)
 Sets the Z component of the Rigidbody's velocity.
static void SetVelocityTo (this Rigidbody rb, Vector3 targetVelocity)
 Sets the Rigidbody's velocity to a target vector.
static void SetRandomVelocity (this Rigidbody rb, float minSpeed, float maxSpeed)
 Sets a random velocity with speed between the specified minimum and maximum.
static void ResetVelocity (this Rigidbody rb)
 Resets the Rigidbody's linear velocity to zero.
static void ResetAngularVelocity (this Rigidbody rb)
 Resets the Rigidbody's angular velocity to zero.
static void AddDampedForce (this Rigidbody rb, Vector3 force, float damping)
 Applies a damped force to the Rigidbody.
static void ApplyImpulseAtPoint (this Rigidbody rb, Vector3 impulse, Vector3 point)
 Applies an impulse force at a specific point.
static void ApplyForceByMass (this Rigidbody rb, Vector3 force)
 Applies a force scaled by the Rigidbody's mass.
static void ApplyJumpForce (this Rigidbody rb, float jumpForce)
 Applies an upward impulse to simulate a jump.
static void ApplyForceInDirectionOfVelocity (this Rigidbody rb, float forceAmount)
 Applies force in the direction of current velocity.
static void ApplyKnockbackForce (this Rigidbody rb, Vector3 impactPoint, float knockbackStrength)
 Applies a knockback impulse away from a given impact point.
static void ApplyRandomTorque (this Rigidbody rb, float torqueAmount)
 Applies random rotational torque to the Rigidbody.
static void RotateToAngle (this Rigidbody rb, Vector3 targetDirection, float speed)
 Rotates the Rigidbody to face a target direction over time.
static void RotateToAlignWithTarget (this Rigidbody rb, Vector3 targetPosition, float torqueAmount)
 Rotates the Rigidbody using torque to align with a target position.
static void ApplySpin (this Rigidbody rb, float torqueAmount)
 Applies continuous torque around the Y-axis.
static void SetPositionDirectly (this Rigidbody rb, Vector3 position)
 Directly sets the Rigidbody's position.
static bool IsMoving (this Rigidbody rb)
 Checks whether the Rigidbody is currently moving.
static bool IsMovingDown (this Rigidbody rb)
 Checks whether the Rigidbody is currently moving downwards.
static float GetSpeedInForwardDirection (this Rigidbody rb)
 Gets the speed in the forward direction of the Rigidbody.
static void FreezeMovementInDirection (this Rigidbody rb, Vector3 direction)
 Restricts Rigidbody movement to a single direction.
static void StopAtPosition (this Rigidbody rb, Vector3 position)
 Stops the Rigidbody when it reaches a specified position.
static void ApplyStopImpulse (this Rigidbody rb)
 Applies an impulse opposite to current velocity to bring Rigidbody to a stop.
static void ResetAll (this Rigidbody rb)
 Resets the Rigidbody's position, velocity, and angular velocity.
static void IgnoreGravityForTime (this Rigidbody rb, float time, MonoBehaviour context)
 Temporarily disables gravity on a Rigidbody for a set duration.

Static Private Member Functions

static IEnumerator RestoreGravityAfterTime (Rigidbody rb, float time)

Detailed Description

Extension methods for Unity's Rigidbody component, providing utility methods for velocity, forces, rotation, and movement control.

Member Function Documentation

◆ AddDampedForce()

void IuvoUnity.Extensions.RigidbodyExtensions.AddDampedForce ( this Rigidbody rb,
Vector3 force,
float damping )
static

Applies a damped force to the Rigidbody.

Parameters
rbThe Rigidbody to apply force to.
forceThe base force vector.
dampingThe damping factor to reduce the force.
98 {
99 rb.AddForce(force * damping);
100 }

◆ ApplyForceByMass()

void IuvoUnity.Extensions.RigidbodyExtensions.ApplyForceByMass ( this Rigidbody rb,
Vector3 force )
static

Applies a force scaled by the Rigidbody's mass.

Parameters
rbThe Rigidbody to apply force to.
forceThe base force vector.
119 {
120 rb.AddForce(force * rb.mass, ForceMode.Force);
121 }

◆ ApplyForceInDirectionOfVelocity()

void IuvoUnity.Extensions.RigidbodyExtensions.ApplyForceInDirectionOfVelocity ( this Rigidbody rb,
float forceAmount )
static

Applies force in the direction of current velocity.

Parameters
rbThe Rigidbody to modify.
forceAmountThe magnitude of the force to apply.
139 {
140 if (rb.linearVelocity.sqrMagnitude > 0.01f)
141 rb.AddForce(rb.linearVelocity.normalized * forceAmount, ForceMode.VelocityChange);
142 }

◆ ApplyImpulseAtPoint()

void IuvoUnity.Extensions.RigidbodyExtensions.ApplyImpulseAtPoint ( this Rigidbody rb,
Vector3 impulse,
Vector3 point )
static

Applies an impulse force at a specific point.

Parameters
rbThe Rigidbody to apply the impulse to.
impulseThe impulse vector.
pointThe world position where the impulse is applied.
109 {
110 rb.AddForceAtPosition(impulse, point, ForceMode.Impulse);
111 }

◆ ApplyJumpForce()

void IuvoUnity.Extensions.RigidbodyExtensions.ApplyJumpForce ( this Rigidbody rb,
float jumpForce )
static

Applies an upward impulse to simulate a jump.

Parameters
rbThe Rigidbody to apply the force to.
jumpForceThe magnitude of the jump force.
129 {
130 rb.AddForce(Vector3.up * jumpForce * rb.mass, ForceMode.Impulse);
131 }

◆ ApplyKnockbackForce()

void IuvoUnity.Extensions.RigidbodyExtensions.ApplyKnockbackForce ( this Rigidbody rb,
Vector3 impactPoint,
float knockbackStrength )
static

Applies a knockback impulse away from a given impact point.

Parameters
rbThe Rigidbody to knock back.
impactPointThe point of impact.
knockbackStrengthThe strength of the knockback.
151 {
152 Vector3 direction = (rb.position - impactPoint).normalized;
153 rb.AddForce(direction * knockbackStrength, ForceMode.Impulse);
154 }

◆ ApplyRandomTorque()

void IuvoUnity.Extensions.RigidbodyExtensions.ApplyRandomTorque ( this Rigidbody rb,
float torqueAmount )
static

Applies random rotational torque to the Rigidbody.

Parameters
rbThe Rigidbody to apply torque to.
torqueAmountThe amount of torque to apply.
162 {
163 rb.AddTorque(UnityEngine.Random.onUnitSphere * torqueAmount, ForceMode.Impulse);
164 }

◆ ApplySpin()

void IuvoUnity.Extensions.RigidbodyExtensions.ApplySpin ( this Rigidbody rb,
float torqueAmount )
static

Applies continuous torque around the Y-axis.

Parameters
rbThe Rigidbody to spin.
torqueAmountThe amount of Y-axis torque to apply.
201 {
202 rb.AddTorque(Vector3.up * torqueAmount, ForceMode.VelocityChange);
203 }

◆ ApplyStopImpulse()

void IuvoUnity.Extensions.RigidbodyExtensions.ApplyStopImpulse ( this Rigidbody rb)
static

Applies an impulse opposite to current velocity to bring Rigidbody to a stop.

Parameters
rbThe Rigidbody to stop.
277 {
278 rb.AddForce(-rb.linearVelocity, ForceMode.Impulse);
279 }

◆ FreezeMovementInDirection()

void IuvoUnity.Extensions.RigidbodyExtensions.FreezeMovementInDirection ( this Rigidbody rb,
Vector3 direction )
static

Restricts Rigidbody movement to a single direction.

Parameters
rbThe Rigidbody to modify.
directionThe direction to allow movement in.
255 {
256 rb.linearVelocity = Vector3.Project(rb.linearVelocity, direction.normalized);
257 }

◆ GetSpeedInForwardDirection()

float IuvoUnity.Extensions.RigidbodyExtensions.GetSpeedInForwardDirection ( this Rigidbody rb)
static

Gets the speed in the forward direction of the Rigidbody.

Parameters
rbThe Rigidbody to evaluate.
Returns
The speed in forward direction (dot product).
245 {
246 return Vector3.Dot(rb.linearVelocity, rb.transform.forward);
247 }

◆ IgnoreGravityForTime()

void IuvoUnity.Extensions.RigidbodyExtensions.IgnoreGravityForTime ( this Rigidbody rb,
float time,
MonoBehaviour context )
static

Temporarily disables gravity on a Rigidbody for a set duration.

Parameters
rbThe Rigidbody to modify.
timeDuration in seconds before restoring gravity.
contextA MonoBehaviour context to start the coroutine.

This method requires a MonoBehaviour to start the coroutine.

304 {
305 context.StartCoroutine(RestoreGravityAfterTime(rb, time));
306 }

References RestoreGravityAfterTime().

◆ IsMoving()

bool IuvoUnity.Extensions.RigidbodyExtensions.IsMoving ( this Rigidbody rb)
static

Checks whether the Rigidbody is currently moving.

Parameters
rbThe Rigidbody to check.
Returns
True if velocity magnitude is greater than 0.1.
225 {
226 return rb.linearVelocity.magnitude > 0.1f;
227 }

◆ IsMovingDown()

bool IuvoUnity.Extensions.RigidbodyExtensions.IsMovingDown ( this Rigidbody rb)
static

Checks whether the Rigidbody is currently moving downwards.

Parameters
rbThe Rigidbody to check.
Returns
True if Y velocity is less than 0.
235 {
236 return rb.linearVelocity.y < 0;
237 }

◆ ResetAll()

void IuvoUnity.Extensions.RigidbodyExtensions.ResetAll ( this Rigidbody rb)
static

Resets the Rigidbody's position, velocity, and angular velocity.

Parameters
rbThe Rigidbody to reset.
286 {
287 rb.position = Vector3.zero;
288 rb.linearVelocity = Vector3.zero;
289 rb.angularVelocity = Vector3.zero;
290 }

◆ ResetAngularVelocity()

void IuvoUnity.Extensions.RigidbodyExtensions.ResetAngularVelocity ( this Rigidbody rb)
static

Resets the Rigidbody's angular velocity to zero.

Parameters
rbThe Rigidbody to modify.
83 {
84 rb.angularVelocity = Vector3.zero;
85 }

◆ ResetVelocity()

void IuvoUnity.Extensions.RigidbodyExtensions.ResetVelocity ( this Rigidbody rb)
static

Resets the Rigidbody's linear velocity to zero.

Parameters
rbThe Rigidbody to modify.
74 {
75 rb.linearVelocity = Vector3.zero;
76 }

◆ RestoreGravityAfterTime()

IEnumerator IuvoUnity.Extensions.RigidbodyExtensions.RestoreGravityAfterTime ( Rigidbody rb,
float time )
staticprivate
309 {
310 rb.useGravity = false;
311 yield return new WaitForSeconds(time);
312 rb.useGravity = true;
313 }

Referenced by IgnoreGravityForTime().

◆ RotateToAlignWithTarget()

void IuvoUnity.Extensions.RigidbodyExtensions.RotateToAlignWithTarget ( this Rigidbody rb,
Vector3 targetPosition,
float torqueAmount )
static

Rotates the Rigidbody using torque to align with a target position.

Parameters
rbThe Rigidbody to rotate.
targetPositionThe world position to look at.
torqueAmountThe torque strength to apply.
189 {
190 Vector3 directionToTarget = (targetPosition - rb.position).normalized;
191 Vector3 torque = Vector3.Cross(rb.transform.forward, directionToTarget);
192 rb.AddTorque(torque * torqueAmount);
193 }

◆ RotateToAngle()

void IuvoUnity.Extensions.RigidbodyExtensions.RotateToAngle ( this Rigidbody rb,
Vector3 targetDirection,
float speed )
static

Rotates the Rigidbody to face a target direction over time.

Parameters
rbThe Rigidbody to rotate.
targetDirectionThe world direction to face.
speedRotation speed in radians per second.
177 {
178 Vector3 newDirection = Vector3.RotateTowards(rb.transform.forward, targetDirection, speed * Time.deltaTime, 0f);
179 rb.rotation = Quaternion.LookRotation(newDirection);
180 }

◆ SetPositionDirectly()

void IuvoUnity.Extensions.RigidbodyExtensions.SetPositionDirectly ( this Rigidbody rb,
Vector3 position )
static

Directly sets the Rigidbody's position.

Parameters
rbThe Rigidbody to move.
positionThe new world position.
215 {
216 rb.position = position;
217 }

◆ SetRandomVelocity()

void IuvoUnity.Extensions.RigidbodyExtensions.SetRandomVelocity ( this Rigidbody rb,
float minSpeed,
float maxSpeed )
static

Sets a random velocity with speed between the specified minimum and maximum.

Parameters
rbThe Rigidbody to modify.
minSpeedMinimum speed.
maxSpeedMaximum speed.
64 {
65 float speed = UnityEngine.Random.Range(minSpeed, maxSpeed);
66 rb.linearVelocity = UnityEngine.Random.onUnitSphere * speed;
67 }

◆ SetVelocityTo()

void IuvoUnity.Extensions.RigidbodyExtensions.SetVelocityTo ( this Rigidbody rb,
Vector3 targetVelocity )
static

Sets the Rigidbody's velocity to a target vector.

Parameters
rbThe Rigidbody to modify.
targetVelocityThe new velocity vector.
53 {
54 rb.linearVelocity = targetVelocity;
55 }

◆ SetVelocityX()

void IuvoUnity.Extensions.RigidbodyExtensions.SetVelocityX ( this Rigidbody rb,
float velocity )
static

Sets the X component of the Rigidbody's velocity.

Parameters
rbThe Rigidbody to modify.
velocityThe X velocity to set.
23 {
24 rb.linearVelocity = new Vector3(velocity, rb.linearVelocity.y, rb.linearVelocity.z);
25 }

◆ SetVelocityY()

void IuvoUnity.Extensions.RigidbodyExtensions.SetVelocityY ( this Rigidbody rb,
float velocity )
static

Sets the Y component of the Rigidbody's velocity.

Parameters
rbThe Rigidbody to modify.
velocityThe Y velocity to set.
33 {
34 rb.linearVelocity = new Vector3(rb.linearVelocity.x, velocity, rb.linearVelocity.z);
35 }

◆ SetVelocityZ()

void IuvoUnity.Extensions.RigidbodyExtensions.SetVelocityZ ( this Rigidbody rb,
float velocity )
static

Sets the Z component of the Rigidbody's velocity.

Parameters
rbThe Rigidbody to modify.
velocityThe Z velocity to set.
43 {
44 rb.linearVelocity = new Vector3(rb.linearVelocity.x, rb.linearVelocity.y, velocity);
45 }

◆ StopAtPosition()

void IuvoUnity.Extensions.RigidbodyExtensions.StopAtPosition ( this Rigidbody rb,
Vector3 position )
static

Stops the Rigidbody when it reaches a specified position.

Parameters
rbThe Rigidbody to stop.
positionThe target position.
265 {
266 if (Vector3.Distance(rb.position, position) < 0.1f)
267 {
268 rb.linearVelocity = Vector3.zero;
269 }
270 }

The documentation for this class was generated from the following file: