So, I am trying to implement a generic moving platform script for Unity 5 that works with pretty much anything. I have managed to get one that does some very basic movement for rigidbodies that fall on top, however trying to add any of the Unity 5 character controllers creates a glitchy situation
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MovingPlatform : MonoBehaviour {
[System.NonSerialized]
private List<GameObject> others = new List<GameObject>();
Vector3 lastPos;
void Start () {
lastPos = transform.position;
}
void OnCollisionEnter(Collision _collision) {
if (others.Contains(_collision.gameObject))
return;
others.Add(_collision.gameObject);
}
void OnCollisionExit(Collision _collision) {
if (others.Contains(_collision.gameObject))
others.Remove(_collision.gameObject);
}
void FixedUpdate() {
foreach(GameObject gobj in others) {
gobj.transform.Translate(transform.position - lastPos);
}
lastPos = transform.position;
}
}
So I would like to keep the solution as basic as possible, but I need some help getting it to work with the various “Standard Assets” character controllers in Unity 5 for a start since they use a weird combination of root motion, rigidbodies, and built-in character controllers, it is causing some real problems.
Parenting is not an option, this must play well with existing hierarchies using math & physics
Any insight into this would be greatly appreciated!