You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
FlyingFred/Assets/Scripts/CharacterController.cs

85 lines
2.5 KiB
C#

using CnControls;
using DG.Tweening;
using UnityEngine;
public class CharacterController : MonoBehaviour
{
public Rigidbody characterRigidbody;
public Rigidbody connectedBone;
public float swingForce = 10f;
public float Health;
public HingeJoint SpineHingeJoint;
public Transform boneTransform;
public Rigidbody[] ragdollRigidbodies;
public Collider[] ragdollColliders;
public Animator CharacterAnimator;
bool bonefollower;
Vector3 offset;
private void Start()
{
Invoke(nameof(Rotator), 3);
ragdollRigidbodies = GetComponentsInChildren<Rigidbody>();
ragdollColliders = GetComponentsInChildren<Collider>();
DisableRagdoll();
bonefollower = true;
offset=transform.position- connectedBone.transform.position;
}
public void DisableRagdoll()
{
foreach (Rigidbody rb in ragdollRigidbodies)
{
rb.isKinematic = true; // Disable physics on rigidbodies
}
foreach (Collider col in ragdollColliders)
{
col.enabled = false; // Disable colliders
}
}
public void EnableRagdoll()
{
CharacterAnimator.enabled = false;
foreach (Rigidbody rb in ragdollRigidbodies)
{
rb.isKinematic = false; // Enable physics on rigidbodies
}
foreach (Collider col in ragdollColliders)
{
col.enabled = true; // Enable colliders
}
}
void Rotator()
{
transform.DOLocalRotate(new Vector3(90, 180, 0), 0.6f);
transform.DOLocalMoveZ(0.75f, 0.6f).OnComplete(() =>
{
bonefollower = false;
EnableRagdoll();
SpineHingeJoint.connectedBody = connectedBone;
});
//boneTransform.transform.DOLocalMoveZ(0, 1.25f);
//boneTransform.transform.DOLocalRotate(Vector3.zero, 1.25f);
}
void Update()
{
// Control the character with horizontal input
float input = CnInputManager.GetAxis("Horizontal");
characterRigidbody.AddForce(Vector2.right * input * swingForce);
if(bonefollower)
{
transform.position = connectedBone.position + offset;
}
}
private void OnCollisionEnter(Collision collision)
{
Debug.Log("collision with: " + collision.gameObject.name);
if (collision.gameObject.layer == LayerMask.NameToLayer("Obstacle"))
{
Health -= 0.5f;
UIManager.instance.UpdateHealth();
}
}
}