97 lines
2.4 KiB
C#
97 lines
2.4 KiB
C#
//----------------------------------------------
|
|
// 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;
|
|
|
|
/// <summary>
|
|
/// Operates the shredder in the damage demo scene.
|
|
/// </summary>
|
|
[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<Rigidbody>();
|
|
|
|
// Creating hinge with configurable joint.
|
|
CreateHinge();
|
|
|
|
}
|
|
|
|
private void FixedUpdate() {
|
|
|
|
// If no rigid, return.
|
|
if (!rigid)
|
|
return;
|
|
|
|
// Apply force.
|
|
rigid.AddRelativeTorque(direction * force, ForceMode.Acceleration);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates hinge with configurable joint.
|
|
/// </summary>
|
|
private void CreateHinge() {
|
|
|
|
GameObject hinge = new GameObject("Hinge_" + transform.name);
|
|
hinge.transform.position = hingePoint.position;
|
|
hinge.transform.rotation = hingePoint.rotation;
|
|
|
|
Rigidbody hingeRigid = hinge.AddComponent<Rigidbody>();
|
|
hingeRigid.isKinematic = true;
|
|
hingeRigid.useGravity = false;
|
|
|
|
AttachHinge(hingeRigid);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets connected body of the configurable joint.
|
|
/// </summary>
|
|
/// <param name="hingeRigid"></param>
|
|
private void AttachHinge(Rigidbody hingeRigid) {
|
|
|
|
ConfigurableJoint joint = GetComponent<ConfigurableJoint>();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|