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.CameraExtensions Class Reference

Static Public Member Functions

static void Initialize (MonoBehaviour runner)
 Initializes the CameraExtensions with a coroutine runner.
static void ShakeScreen (this Camera camera, float magnitude, float duration)
 Shakes the camera screen by applying random offsets to its position.
static void SetBackgroundColor (this Camera camera, Color color)
 Sets the camera's background color.
static void FadeOut (this Camera camera, float duration)
 Fades the camera's background to transparent over a given duration.
static void FadeIn (this Camera camera, float duration)
 Fades the camera's background from transparent to opaque over a given duration.
static void MoveToPosition (this Camera camera, Vector3 targetPosition, float speed)
 Moves the camera towards a target position at a specified speed.
static void RotateToRotation (this Camera camera, Quaternion targetRotation, float speed)
 Rotates the camera towards a target rotation at a specified speed.
static void LookAt (this Camera camera, Transform target)
 Makes the camera look at a specified target.
static void LookAt (this Camera camera, Vector3 target)
 Makes the camera look at a specified target position.
static void LookAt (this Camera camera, GameObject target)
 Makes the camera look at a specified target GameObject.
static void Follow (this Camera camera, Transform target, Vector3 offset)
 Makes the camera follow a specified target with an offset.
static void ZoomIn (this Camera camera, float amount)
 Zooms in the camera by reducing its field of view.
static void ZoomOut (this Camera camera, float amount)
 Zooms out the camera by increasing its field of view.
static void SetPosition (this Camera camera, Vector3 position)
 Sets the camera's position to a specific value.
static void SetRotation (this Camera camera, Quaternion rotation)
 Sets the camera's rotation to a specific value.
static void LockRotationAxis (this Camera camera, Vector3 lockedAxis)
 Locks the camera's rotation on specific axes.
static void SetRenderTarget (this Camera camera, RenderTexture renderTexture)
 Sets the camera's render target to a specific RenderTexture.
static Ray GetCenterRay (this Camera camera)
 Gets a ray from the center of the camera's viewport.
static ? RaycastHit RaycastFromCenter (this Camera camera, float maxDistance, LayerMask layerMask)
 Performs a raycast from the center of the camera's viewport.

Static Private Member Functions

static IEnumerator ShakeScreenCoroutine (Camera camera, float magnitude, float duration)
static IEnumerator FadeOutCoroutine (Camera camera, float duration)
static IEnumerator FadeInCoroutine (Camera camera, float duration)

Static Private Attributes

static MonoBehaviour coroutineRunner

Member Function Documentation

◆ FadeIn()

void IuvoUnity.Extensions.CameraExtensions.FadeIn ( this Camera camera,
float duration )
static

Fades the camera's background from transparent to opaque over a given duration.

Parameters
cameraThe camera to fade.
durationThe time, in seconds, for the fade effect.
87 {
88 coroutineRunner.StartCoroutine(FadeInCoroutine(camera, duration));
89 }

References coroutineRunner, and FadeInCoroutine().

◆ FadeInCoroutine()

IEnumerator IuvoUnity.Extensions.CameraExtensions.FadeInCoroutine ( Camera camera,
float duration )
staticprivate
92 {
93 float startAlpha = camera.backgroundColor.a;
94 float timer = 0;
95 while (timer < duration)
96 {
97 Color newColor = camera.backgroundColor;
98 newColor.a = Mathf.Lerp(startAlpha, 1, timer / duration);
99 camera.backgroundColor = newColor;
100 timer += Time.deltaTime;
101 yield return null;
102 }
103 }

Referenced by FadeIn().

◆ FadeOut()

void IuvoUnity.Extensions.CameraExtensions.FadeOut ( this Camera camera,
float duration )
static

Fades the camera's background to transparent over a given duration.

Parameters
cameraThe camera to fade.
durationThe time, in seconds, for the fade effect.
63 {
64 coroutineRunner.StartCoroutine(FadeOutCoroutine(camera, duration));
65 }

References coroutineRunner, and FadeOutCoroutine().

◆ FadeOutCoroutine()

IEnumerator IuvoUnity.Extensions.CameraExtensions.FadeOutCoroutine ( Camera camera,
float duration )
staticprivate
68 {
69 float startAlpha = camera.backgroundColor.a;
70 float timer = 0;
71 while (timer < duration)
72 {
73 Color newColor = camera.backgroundColor;
74 newColor.a = Mathf.Lerp(startAlpha, 0, timer / duration);
75 camera.backgroundColor = newColor;
76 timer += Time.deltaTime;
77 yield return null;
78 }
79 }

Referenced by FadeOut().

◆ Follow()

void IuvoUnity.Extensions.CameraExtensions.Follow ( this Camera camera,
Transform target,
Vector3 offset )
static

Makes the camera follow a specified target with an offset.

Parameters
cameraThe camera to follow the target.
targetThe target to follow.
offsetThe offset from the target.
164 {
165 camera.transform.position = target.position + offset;
166 }

◆ GetCenterRay()

Ray IuvoUnity.Extensions.CameraExtensions.GetCenterRay ( this Camera camera)
static

Gets a ray from the center of the camera's viewport.

Parameters
cameraThe camera from which the ray is cast.
Returns
A ray originating from the center of the camera's viewport.
240 {
241 return camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
242 }

◆ Initialize()

void IuvoUnity.Extensions.CameraExtensions.Initialize ( MonoBehaviour runner)
static

Initializes the CameraExtensions with a coroutine runner.

Parameters
runnerThe MonoBehaviour instance used to run coroutines.
17 {
18 coroutineRunner = runner;
19 }

References coroutineRunner.

◆ LockRotationAxis()

void IuvoUnity.Extensions.CameraExtensions.LockRotationAxis ( this Camera camera,
Vector3 lockedAxis )
static

Locks the camera's rotation on specific axes.

Parameters
cameraThe camera whose rotation axes are to be locked.
lockedAxisThe axes to lock (x, y, z).
216 {
217 camera.transform.eulerAngles = new Vector3(camera.transform.eulerAngles.x * lockedAxis.x,
218 camera.transform.eulerAngles.y * lockedAxis.y,
219 camera.transform.eulerAngles.z * lockedAxis.z);
220 }

◆ LookAt() [1/3]

void IuvoUnity.Extensions.CameraExtensions.LookAt ( this Camera camera,
GameObject target )
static

Makes the camera look at a specified target GameObject.

Parameters
cameraThe camera to look at the target.
targetThe target GameObject to look at.
153 {
154 camera.transform.LookAt(target.transform);
155 }

◆ LookAt() [2/3]

void IuvoUnity.Extensions.CameraExtensions.LookAt ( this Camera camera,
Transform target )
static

Makes the camera look at a specified target.

Parameters
cameraThe camera to look at the target.
targetThe target to look at.
133 {
134 camera.transform.LookAt(target);
135 }

◆ LookAt() [3/3]

void IuvoUnity.Extensions.CameraExtensions.LookAt ( this Camera camera,
Vector3 target )
static

Makes the camera look at a specified target position.

Parameters
cameraThe camera to look at the target.
targetThe target position to look at.
143 {
144 camera.transform.LookAt(target);
145 }

◆ MoveToPosition()

void IuvoUnity.Extensions.CameraExtensions.MoveToPosition ( this Camera camera,
Vector3 targetPosition,
float speed )
static

Moves the camera towards a target position at a specified speed.

Parameters
cameraThe camera to move.
targetPositionThe target position to move towards.
speedThe speed of the movement.
112 {
113 camera.transform.position = Vector3.Lerp(camera.transform.position, targetPosition, speed * Time.deltaTime);
114 }

◆ RaycastFromCenter()

? RaycastHit IuvoUnity.Extensions.CameraExtensions.RaycastFromCenter ( this Camera camera,
float maxDistance,
LayerMask layerMask )
static

Performs a raycast from the center of the camera's viewport.

Parameters
cameraThe camera to perform the raycast from.
maxDistanceThe maximum distance the ray should check for collisions.
layerMaskThe layers to include in the raycast.
Returns
A RaycastHit containing information about the hit, or null if no hit occurred.
252 {
253 Ray ray = camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
254 if (Physics.Raycast(ray, out RaycastHit hit, maxDistance, layerMask))
255 return hit;
256 return null;
257 }

◆ RotateToRotation()

void IuvoUnity.Extensions.CameraExtensions.RotateToRotation ( this Camera camera,
Quaternion targetRotation,
float speed )
static

Rotates the camera towards a target rotation at a specified speed.

Parameters
cameraThe camera to rotate.
targetRotationThe target rotation to rotate towards.
speedThe speed of the rotation.
123 {
124 camera.transform.rotation = Quaternion.Lerp(camera.transform.rotation, targetRotation, speed * Time.deltaTime);
125 }

◆ SetBackgroundColor()

void IuvoUnity.Extensions.CameraExtensions.SetBackgroundColor ( this Camera camera,
Color color )
static

Sets the camera's background color.

Parameters
cameraThe camera whose background color is to be set.
colorThe color to set as the background.
53 {
54 camera.backgroundColor = color;
55 }

◆ SetPosition()

void IuvoUnity.Extensions.CameraExtensions.SetPosition ( this Camera camera,
Vector3 position )
static

Sets the camera's position to a specific value.

Parameters
cameraThe camera to set the position for.
positionThe position to set for the camera.
196 {
197 camera.transform.position = position;
198 }

◆ SetRenderTarget()

void IuvoUnity.Extensions.CameraExtensions.SetRenderTarget ( this Camera camera,
RenderTexture renderTexture )
static

Sets the camera's render target to a specific RenderTexture.

Parameters
cameraThe camera to set the render target for.
renderTextureThe RenderTexture to use as the render target.
230 {
231 camera.targetTexture = renderTexture;
232 }

◆ SetRotation()

void IuvoUnity.Extensions.CameraExtensions.SetRotation ( this Camera camera,
Quaternion rotation )
static

Sets the camera's rotation to a specific value.

Parameters
cameraThe camera to set the rotation for.
rotationThe rotation to set for the camera.
206 {
207 camera.transform.rotation = rotation;
208 }

◆ ShakeScreen()

void IuvoUnity.Extensions.CameraExtensions.ShakeScreen ( this Camera camera,
float magnitude,
float duration )
static

Shakes the camera screen by applying random offsets to its position.

Parameters
cameraThe camera to shake.
magnitudeThe magnitude of the shake (larger values produce a stronger shake).
durationHow long the shake will last, in seconds.
28 {
29 coroutineRunner.StartCoroutine(ShakeScreenCoroutine(camera, magnitude, duration));
30 }

References coroutineRunner, and ShakeScreenCoroutine().

◆ ShakeScreenCoroutine()

IEnumerator IuvoUnity.Extensions.CameraExtensions.ShakeScreenCoroutine ( Camera camera,
float magnitude,
float duration )
staticprivate
33 {
34 Vector3 originalPosition = camera.transform.position;
35 float timeElapsed = 0f;
36
37 while (timeElapsed < duration)
38 {
39 camera.transform.position = originalPosition + UnityEngine.Random.insideUnitSphere * magnitude;
40 timeElapsed += Time.deltaTime;
41 yield return null;
42 }
43 camera.transform.position = originalPosition;
44 }

Referenced by ShakeScreen().

◆ ZoomIn()

void IuvoUnity.Extensions.CameraExtensions.ZoomIn ( this Camera camera,
float amount )
static

Zooms in the camera by reducing its field of view.

Parameters
cameraThe camera to zoom in.
amountThe amount to zoom in, reducing the field of view.
176 {
177 camera.fieldOfView -= amount;
178 }

◆ ZoomOut()

void IuvoUnity.Extensions.CameraExtensions.ZoomOut ( this Camera camera,
float amount )
static

Zooms out the camera by increasing its field of view.

Parameters
cameraThe camera to zoom out.
amountThe amount to zoom out, increasing the field of view.
186 {
187 camera.fieldOfView += amount;
188 }

Member Data Documentation

◆ coroutineRunner

MonoBehaviour IuvoUnity.Extensions.CameraExtensions.coroutineRunner
staticprivate

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