using System.Collections; using UnityEngine; namespace Passer { /// /// Utility functions to move transformes in the world. /// public class Transportation : MonoBehaviour { public float forwardSpeed = 1; public float sidewardSpeed = 1; public float rotationalSpeed = 60; /// /// Moves the transform along the local Z-axis with the indicated speed /// /// Should be called every frame for continuous movement. /// public virtual void MoveForward(float z) { transform.MoveForward(forwardSpeed * z * Time.deltaTime); } /// /// Moves the transform along the local X-axis with the indicated speed /// /// Should be called every frame for continuous movement. /// public virtual void MoveSideward(float x) { transform.MoveSidewards(sidewardSpeed * x * Time.deltaTime); } /// /// Rotates this transform with the indicated speed around the Y axis. /// /// Should be called every frame for continuous rotation. /// The speed in degrees per second public virtual void Rotate(float angularSpeed) { transform.Turn(rotationalSpeed * angularSpeed * Time.deltaTime); } /// /// Quickly moves this transform to the targetPosition /// /// This is a coroutine which will move the transform using DashCoroutine /// It does not need to be called every frame. /// public void Dash(Vector3 targetPosition) { StartCoroutine(TransformMovements.DashCoroutine(transform, targetPosition)); } /// /// Teleport this transform to the targetPosition /// /// The target position public void Teleport(Vector3 targetPosition) { TransformMovements.Teleport(this.transform, targetPosition); } /// /// Teleport this transform forward by 1 meter /// public void Teleport() { TransformMovements.Teleport(this.transform, this.transform.position + this.transform.forward); } } public static class TransformMovements { /// /// Move the transform forward /// /// The transform to move /// The distance to move. Negative is moving backwards public static void MoveForward(this Transform transform, float z) { transform.Translate(Vector3.forward * z); } /// /// Move the transform sidewards /// /// The transform to move /// The distance to move. Positive is move to the right public static void MoveSidewards(this Transform transform, float x) { transform.Translate(Vector3.right * x); } /// /// Rotate the transform along the Y axis /// /// The transform to turn /// The angle in degrees. Positive is rotate to the right public static void Turn(this Transform transform, float angle) { if (angle != 0) transform.Rotate(0, angle, 0); } /// /// Teleport the transform to the target position /// /// The transform to teleport /// The target position to teleport to public static void Teleport(this Transform transform, Vector3 targetPosition) { //Rigidbody transformRigidbody = transform.GetComponent(); //if (transformRigidbody != null) // transformRigidbody.MovePosition(targetPosition); //else transform.position = targetPosition; } private const float minSpeedMps = 50.0f; // clamped to minimum speed 50m/s to avoid sickness private const float normalLerpTime = 0.1f; // 100ms for every dash above minDistanceForNormalLerp private const float minDistanceForNormalLerp = minSpeedMps * normalLerpTime; // default values give 5.0f; private static float lerpTime = 5; //0.1f; /// /// Dashes the transform to the target position /// This needs to be called using MonoBehaviour.StartCoroutine /// /// The transform to move /// The target position of the dash movement /// Coroutine result public static IEnumerator DashCoroutine(Transform transform, Vector3 targetPosition) { float maxDistance = Vector3.Distance(transform.position, targetPosition - transform.forward * 0.5f); if (maxDistance >= minDistanceForNormalLerp) lerpTime = normalLerpTime; // fixed time for all bigger dashes else lerpTime = maxDistance / minSpeedMps; // clamped to speed for small dashes Vector3 startPosition = transform.position; float elapsedTime = 0; float t = 0; while (t < 1) { transform.position = Vector3.Lerp(startPosition, targetPosition, t); elapsedTime += Time.deltaTime; t = elapsedTime / lerpTime; yield return new WaitForEndOfFrame(); } transform.position = targetPosition; } } }