Skip to content

Instantly share code, notes, and snippets.

@adammyhre
Last active July 21, 2024 20:41
Show Gist options
  • Save adammyhre/7b4da936affce387fbbd60e68acc34f6 to your computer and use it in GitHub Desktop.
Save adammyhre/7b4da936affce387fbbd60e68acc34f6 to your computer and use it in GitHub Desktop.
Player Controller Sandbox Scripts
using UnityEngine;
using UnityUtils;
[RequireComponent(typeof(TriggerArea))]
public class GravityWell : MonoBehaviour {
TriggerArea triggerArea;
void Start() {
triggerArea = GetComponent<TriggerArea>();
}
void FixedUpdate() {
for (var i = 0; i < triggerArea.RigidBodies.Count; i++) {
var rb = triggerArea.RigidBodies[i];
var directionToRb = rb.transform.position - transform.position;
Debug.DrawLine(transform.position, rb.transform.position, Color.red);
var projection = Vector3.Project(directionToRb, transform.forward);
Debug.DrawLine(transform.position, transform.position + projection, Color.blue);
var center = projection + transform.position;
Debug.DrawLine(transform.position, center, Color.green);
var directionToCenter = center - rb.transform.position;
Debug.DrawLine(rb.transform.position, center, Color.yellow);
RotateRigidbody(rb, directionToCenter);
}
}
void OnTriggerExit(Collider col) {
if (col.TryGetComponent(out Rigidbody rb)) {
RotateRigidbody(rb, Vector3.up);
var eulerAngles = rb.rotation.eulerAngles.With(x: 0f, z: 0f);
rb.MoveRotation(Quaternion.Euler(eulerAngles));
}
}
void RotateRigidbody(Rigidbody rb, Vector3 targetDirection) {
targetDirection.Normalize();
var rotationDifference = Quaternion.FromToRotation(rb.transform.up, targetDirection);
var finalRotation = rotationDifference * rb.transform.rotation;
rb.MoveRotation(finalRotation);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityUtils; // https://github.com/adammyhre/Unity-Utils
[RequireComponent(typeof(TriggerArea))]
public class MovingPlatform : MonoBehaviour {
[SerializeField] float movementSpeed = 5f;
[SerializeField] float waitTime = 2f;
[SerializeField] bool reverseDirection;
[SerializeField] List<Transform> waypoints = new();
bool isWaiting;
int currentWaypointIndex;
Transform currentWaypoint;
TriggerArea triggerArea;
Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
triggerArea = GetComponent<TriggerArea>();
rb.freezeRotation = true;
rb.useGravity = false;
rb.isKinematic = true;
if (waypoints.Count <= 0) {
Debug.LogWarning($"No waypoints have been assigned to {name}!");
} else {
currentWaypoint = waypoints[currentWaypointIndex];
}
StartCoroutine(WaitRoutine());
StartCoroutine(LateFixedUpdate());
}
IEnumerator WaitRoutine() {
var duration = Helpers.GetWaitForSeconds(waitTime);
while (true) {
if (isWaiting) {
yield return duration;
isWaiting = false;
}
yield return null;
}
}
IEnumerator LateFixedUpdate() {
while (true) {
yield return WaitFor.FixedUpdate;
MovePlatform();
}
}
void UpdateWaypoint() {
currentWaypointIndex += reverseDirection ? -1 : 1;
currentWaypointIndex = (currentWaypointIndex + waypoints.Count) % waypoints.Count;
currentWaypoint = waypoints[currentWaypointIndex];
isWaiting = true;
}
void MovePlatform() {
if (waypoints.Count <= 0 || isWaiting) return;
var toNextWaypoint = currentWaypoint.position - transform.position;
var movement = toNextWaypoint.normalized * (movementSpeed * Time.deltaTime);
if (movement.magnitude >= toNextWaypoint.magnitude || movement.magnitude == 0f) {
rb.transform.position = currentWaypoint.position;
UpdateWaypoint();
} else {
rb.transform.position += movement;
}
for (var i = 0; i < triggerArea.RigidBodies.Count; i++) {
var rigidBody = triggerArea.RigidBodies[i];
rigidBody.MovePosition(rigidBody.position + movement);
}
}
}
using UnityEngine;
public class RightAngleFlip : MonoBehaviour {
void OnTriggerEnter(Collider col) {
if (col.attachedRigidbody != null) {
FlipDirection(transform.forward, col.transform);
}
}
void FlipDirection(Vector3 newUpDirection, Transform tr) {
var angleBetweenUpDirections = Vector3.Angle(newUpDirection, tr.up);
var angleThreshold = 0.001f;
if (angleBetweenUpDirections < angleThreshold) {
return;
}
var rotationDifference = Quaternion.FromToRotation(tr.up, newUpDirection);
tr.rotation = rotationDifference * tr.rotation;
}
}
using System.Collections.Generic;
using UnityEngine;
public class TriggerArea : MonoBehaviour {
readonly List<Rigidbody> rigidBodies = new();
public IReadOnlyList<Rigidbody> RigidBodies => rigidBodies;
void OnTriggerEnter(Collider col) {
if (col.attachedRigidbody != null) {
rigidBodies.Add(col.attachedRigidbody);
}
}
void OnTriggerExit(Collider col) {
if (col.attachedRigidbody != null) {
rigidBodies.Remove(col.attachedRigidbody);
}
}
}
using UnityEngine;
public static class WaitFor {
static readonly WaitForFixedUpdate fixedUpdate = new WaitForFixedUpdate();
public static WaitForFixedUpdate FixedUpdate => fixedUpdate;
static readonly WaitForEndOfFrame endOfFrame = new WaitForEndOfFrame();
public static WaitForEndOfFrame EndOfFrame => endOfFrame;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment