Created
March 18, 2016 20:33
-
-
Save samsamm777/a3c1d6c7ad81a0607806 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using FluffyUnderware.Curvy; | |
using FluffyUnderware.Curvy.Controllers; | |
[System.Serializable] | |
public class ConveyorObject | |
{ | |
public GameObject obj; | |
public float tf; | |
public Vector3 offset; | |
} | |
public class ConveyorBeltMachine : BaseFactoryMachine | |
{ | |
public CurvySpline spline; | |
public float speed = 1.0f; | |
public List<ConveyorObject> conveyorObjects = new List<ConveyorObject>(); | |
public bool snapToMiddle; | |
void FixedUpdate() | |
{ | |
if (spline) { | |
if (isOn) { | |
for (int i = 0; i < conveyorObjects.Count; i++) { | |
// Find the position on the line using the time frame. | |
Vector3 positionOnLine = spline.Interpolate (conveyorObjects[i].tf); | |
// Find the perpendicular direction from the line tangent. This is a vector at a right angle | |
// to the direction of line travel. | |
Vector3 perpendicularDirection = -Vector3.Cross (spline.GetTangentFast(conveyorObjects[i].tf), Vector3.up); | |
// float dot = -Vector3.Dot (conveyorObjects[i].offset, perpendicularDirection); | |
// Debug.Log (dot); | |
// if (dot < 0) { | |
// perpendicularDirection = -perpendicularDirection; | |
// } | |
Vector3 newPosition; | |
if (snapToMiddle) { | |
newPosition = positionOnLine; | |
} else { | |
// The new position is the position on the line, and the right angle direction. We also add the original | |
// offset magnitude so the item stays the same distance from the center as it travels. | |
newPosition = positionOnLine + perpendicularDirection * conveyorObjects [i].offset.magnitude; | |
} | |
Debug.DrawLine (spline.transform.TransformPoint (positionOnLine), spline.transform.TransformPoint (newPosition)); | |
// We need to re-add the original Y position to the calculate position, so the item still remains | |
// on top of the conveyor belt. | |
Vector3 boxY = new Vector3 (0, conveyorObjects [i].offset.y, 0); | |
// Move the rigid body to the newly calculated position. | |
Rigidbody rigidbody = conveyorObjects[i].obj.GetComponent<Rigidbody> (); | |
rigidbody.MovePosition (spline.transform.TransformPoint(newPosition) + boxY); | |
// Increase the timeframe for the object. | |
conveyorObjects[i].tf += conveyorObjects[i].tf * Time.fixedDeltaTime * speed; | |
} | |
} | |
} | |
} | |
void OnCollisionExit(Collision collision) | |
{ | |
for (int i = 0; i < conveyorObjects.Count; i++) { | |
if (collision.gameObject == conveyorObjects[i].obj) { | |
conveyorObjects.Remove (conveyorObjects [i]); | |
return; | |
} | |
} | |
} | |
void OnCollisionEnter(Collision collision) | |
{ | |
Rigidbody rigidbody = collision.gameObject.GetComponent<Rigidbody> (); | |
if (rigidbody) { | |
// get nearest TF(Time Frame) and point on spline | |
Vector3 objectLocalPosition = spline.transform.InverseTransformPoint(collision.transform.position); | |
float startTimeFrame = spline.GetNearestPointTF(objectLocalPosition); | |
Vector3 positionAtTime = spline.Interpolate (startTimeFrame); | |
Vector3 offsetPosition = objectLocalPosition - positionAtTime; | |
ConveyorObject co = new ConveyorObject (); | |
co.obj = collision.gameObject; | |
co.tf = startTimeFrame; | |
co.offset = offsetPosition; | |
conveyorObjects.Add (co); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment