Skip to content

Instantly share code, notes, and snippets.

@nothke
Last active February 17, 2022 15:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nothke/ee83085ba2510246cccb6bed6c6d1ccf to your computer and use it in GitHub Desktop.
Save nothke/ee83085ba2510246cccb6bed6c6d1ccf to your computer and use it in GitHub Desktop.
/// Add this script to the parent of colliders you wish to sync to the root rigidbody
using KinematicCharacterController;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuildKCCPlatform : MonoBehaviour
{
public Transform source;
public bool destroySource = false;
void Start()
{
if (source == null)
source = GetComponentInParent<Transform>();
GameObject go = Instantiate(gameObject);
go.transform.localScale = transform.lossyScale;
Destroy(go.GetComponent<BuildKCCMover>());
var children = go.GetComponentsInChildren<Transform>(); // alloc
for (int i = 0; i < children.Length; i++)
{
Transform child = children[i];
if (!child)
continue;
child.gameObject.layer = 9; // HARDCODED LAYER!!
// if one of the children has a mover, destroy it
if (child.gameObject != go && child.GetComponent<BuildKCCMover>())
Destroy(child.gameObject);
}
var moverTets = go.AddComponent<KCCGhostPlatform>();
moverTets.sourceObject = source;
go.AddComponent<PhysicsMover>();
if (destroySource)
Destroy(gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using KinematicCharacterController;
public class KCCGhostPlatform : MonoBehaviour, IMoverController
{
public Transform sourceObject;
PhysicsMover _mover;
PhysicsMover mover { get { if (!_mover) _mover = GetComponent<PhysicsMover>(); return _mover; } }
private void Start()
{
mover.MoverController = this;
}
public void UpdateMovement(out Vector3 goalPosition, out Quaternion goalRotation, float deltaTime)
{
goalRotation = sourceObject.rotation;
goalPosition = sourceObject.position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment