Commit b6462278 authored by Andrew Van Buren's avatar Andrew Van Buren

Added followable paths.

parent d9bba413
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class FollowPath : MonoBehaviour {
[MenuItem("GameObject/Create Followable Path", false, 10)]
static void CreateCustomGameObject(MenuCommand menuCommand) {
// Create a custom game object
GameObject go = new GameObject("Follow Path");
go.AddComponent<LineRenderer>();
// Ensure it gets reparented if this was a context click (otherwise does nothing)
GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
// Register the creation in the undo system
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
Selection.activeObject = go;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class FollowPath : MonoBehaviour {
public GameObject objectThatFollows;
public bool followAtStart = false;
public bool loop = false;
public float duration = 1; // In seconds
public bool smoothMovement = true;
private LineRenderer lr;
private int direction = 1; // 1 forwards, -1 backwards
// Use this for initialization
void Start () {
lr = GetComponent<LineRenderer>();
if (followAtStart) {
//if (loop) {
// InvokeRepeating("Follow", 0, duration + 0.01f);
//}else {
// Follow();
//}
StartCoroutine(FollowLoop());
}
}
void Follow() {
StartCoroutine(FollowLoop());
}
public IEnumerator FollowLoop() {
int pointIndex = 0;
if (direction < 0) {
// Reversed, start at end
pointIndex = lr.numPositions;
}
float lerp = 0;
int previousInt = -1;
objectThatFollows.transform.position = lr.GetPosition(Mathf.Clamp(pointIndex, 0, lr.numPositions-1));
for (float i = 0; i < duration; i += Time.deltaTime) {
// 0-1 forwards or 1-0 backwards (linear)
lerp = Mathf.Repeat((i/duration) * direction, 1);
// Smooth
float smooth = Mathf.SmoothStep(0, 1, lerp);
// within current/next point
float localLerp = ((smoothMovement ? smooth : lerp) * (lr.numPositions - 1));
int lerpInt = ((direction > 0) ? (int)Mathf.Floor(localLerp) : (int)Mathf.Ceil(localLerp));
if (previousInt < 0) {
// Init previous
previousInt = Mathf.Abs(lerpInt);
}
// Increment
if (lerpInt != previousInt) {
previousInt = lerpInt;
if (pointIndex + direction >= lr.numPositions || pointIndex + direction < 0) {
break;
} else {
pointIndex += direction;
}
}
// Wrap
localLerp = Mathf.Repeat(localLerp, 1) * direction;
// Interpolate
if (pointIndex + direction < lr.numPositions && pointIndex + direction >= 0 && pointIndex < lr.numPositions) {
objectThatFollows.transform.position = Vector3.Lerp(lr.GetPosition(pointIndex), lr.GetPosition(pointIndex + direction), Mathf.Repeat(localLerp, 1));
}
yield return new WaitForEndOfFrame();
}
objectThatFollows.transform.position = lr.GetPosition(pointIndex + direction);
if (loop) {
direction *= -1;
StartCoroutine(FollowLoop());
}
}
}
fileFormatVersion: 2
guid: eca31d3cff9f7a541818958ad4851b14
timeCreated: 1487896640
guid: 472299b06e8408d47ae160cfee4cdb63
timeCreated: 1487898438
licenseType: Free
MonoImporter:
serializedVersion: 2
......
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class _FollowPathEditor : MonoBehaviour {
[MenuItem("GameObject/Create Followable Path", false, 10)]
static void CreateCustomGameObject(MenuCommand menuCommand) {
// Create a custom game object
GameObject go = new GameObject("Path");
LineRenderer lr = go.AddComponent<LineRenderer>();
lr.SetPosition(1, Vector3.right);
lr.widthMultiplier = 0.1f;
go.AddComponent<FollowPath>();
// Ensure it gets reparented if this was a context click (otherwise does nothing)
GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
// Register the creation in the undo system
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
Selection.activeObject = go;
}
}
fileFormatVersion: 2
guid: eca31d3cff9f7a541818958ad4851b14
timeCreated: 1487896640
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment