//----------------------------------------------
// Realistic Car Controller
//
// Copyright © 2014 - 2023 BoneCracker Games
// https://www.bonecrackergames.com
// Buğra Özdoğanlar
//
//----------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// Operates the shredder in the damage demo scene.
///
[RequireComponent(typeof(Rigidbody))]
public class RCC_CrashShredder : MonoBehaviour {
public Transform hingePoint; // Hinge joint.
private Rigidbody rigid; // Rigid.
public Vector3 direction; // Direction of the force.
public float force = 1f; // Strength of the force.
private void Start() {
// Getting rigidbody.
rigid = GetComponent();
// Creating hinge with configurable joint.
CreateHinge();
}
private void FixedUpdate() {
// If no rigid, return.
if (!rigid)
return;
// Apply force.
rigid.AddRelativeTorque(direction * force, ForceMode.Acceleration);
}
///
/// Creates hinge with configurable joint.
///
private void CreateHinge() {
GameObject hinge = new GameObject("Hinge_" + transform.name);
hinge.transform.position = hingePoint.position;
hinge.transform.rotation = hingePoint.rotation;
Rigidbody hingeRigid = hinge.AddComponent();
hingeRigid.isKinematic = true;
hingeRigid.useGravity = false;
AttachHinge(hingeRigid);
}
///
/// Sets connected body of the configurable joint.
///
///
private void AttachHinge(Rigidbody hingeRigid) {
ConfigurableJoint joint = GetComponent();
if (!joint) {
print("Configurable Joint of the " + transform.name + " not found! Be sure this gameobject has Configurable Joint with right config.");
return;
}
joint.autoConfigureConnectedAnchor = false;
joint.connectedBody = hingeRigid;
joint.connectedAnchor = Vector3.zero;
}
private void Reset() {
if (hingePoint == null) {
hingePoint = new GameObject("Hinge Point").transform;
hingePoint.SetParent(transform, false);
}
}
}