Первый коммит
This commit is contained in:
98
Assets/RealisticCarControllerV3/Scripts/RCC_CrashHammer.cs
Normal file
98
Assets/RealisticCarControllerV3/Scripts/RCC_CrashHammer.cs
Normal file
@ -0,0 +1,98 @@
|
||||
//----------------------------------------------
|
||||
// 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 hammer in the damage demo scene.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class RCC_CrashHammer : MonoBehaviour {
|
||||
|
||||
public Transform hingePoint; // Hinge point of the joint.
|
||||
private Rigidbody rigid; // Rigidbody.
|
||||
public Vector3 torque; // Torque.
|
||||
|
||||
public float length = 1f; // Length of the wave.
|
||||
public float speed = 1f; // Speed of the wave.
|
||||
|
||||
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.AddRelativeForce(torque * ((float)Mathf.Sin(Time.time * speed) * length), 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.");
|
||||
enabled = false;
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user