Created
July 10, 2024 09:58
-
-
Save digitalbreed/0addad5afbe9eeb762e656122dee53ee to your computer and use it in GitHub Desktop.
Enhanced RVR's Game Creator 2 / Ragdoll Animator 2 integration from https://www.youtube.com/watch?v=3IepLlMrpzk to take note of the position delta between Controller and Ragdoll. This is to avoid that the Controller is positioned too low upon position swap.
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; | |
public class PositionSwapper : MonoBehaviour | |
{ | |
// GameObject fields for Controller, Mannequin, and Ragdoll | |
public GameObject Controller; | |
public GameObject Mannequin; | |
public GameObject Ragdoll; | |
// Public field for delay | |
public float Delay; | |
// Stores the difference between GC2's character controller and the Ragdoll Animator 2 ragdoll | |
private Vector3 _delta; | |
void Awake() | |
{ | |
// Record delta with a little delay so the active ragdoll comes to rest | |
Invoke("RecordDelta", 0.5f); | |
} | |
private void RecordDelta() | |
{ | |
_delta = Controller.transform.position - Ragdoll.transform.position; | |
} | |
// Public method Execute | |
public void Execute() | |
{ | |
StartCoroutine(ExecuteWithDelay()); | |
} | |
// Coroutine to handle the delay | |
private IEnumerator ExecuteWithDelay() | |
{ | |
// Wait for the specified delay | |
yield return new WaitForSeconds(Delay); | |
// Store the global position and rotation of Ragdoll | |
Vector3 ragdollPosition = Ragdoll.transform.position; | |
Quaternion ragdollRotation = Ragdoll.transform.rotation; | |
// Unparent Ragdoll from Mannequin | |
Ragdoll.transform.parent = null; | |
// Set the global position and rotation of Controller to that of Ragdoll | |
Controller.transform.position = ragdollPosition + _delta; // Add the delta which was previously stored | |
Controller.transform.rotation = ragdollRotation; | |
// Reparent Ragdoll to Mannequin | |
Ragdoll.transform.parent = Mannequin.transform; | |
// Set the local position and rotation of Ragdoll to 0 | |
Ragdoll.transform.localPosition = Vector3.zero; | |
Ragdoll.transform.localRotation = Quaternion.identity; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment