161 lines
3.7 KiB
C#
161 lines
3.7 KiB
C#
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(Rigidbody))]
|
|
public class MegaShapeRBodyPathNew : MonoBehaviour
|
|
{
|
|
public MegaShape path;
|
|
|
|
public int curve;
|
|
|
|
public bool usealpha;
|
|
|
|
public float impulse = 10f;
|
|
|
|
public float inputfrc = 10f;
|
|
|
|
public bool align = true;
|
|
|
|
public float alpha;
|
|
|
|
public float delay = 1f;
|
|
|
|
public float drag;
|
|
|
|
public float jump = 10f;
|
|
|
|
public float breakforce = 100f;
|
|
|
|
public bool connected = true;
|
|
|
|
private Rigidbody rb;
|
|
|
|
private float drive;
|
|
|
|
private float vel;
|
|
|
|
private float tfrc;
|
|
|
|
private Vector3 nps;
|
|
|
|
private void Start()
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
Position();
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
tfrc = 0f;
|
|
if (Input.GetKey(KeyCode.UpArrow))
|
|
{
|
|
tfrc = 0f - inputfrc;
|
|
}
|
|
else if (Input.GetKey(KeyCode.DownArrow))
|
|
{
|
|
tfrc = inputfrc;
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Space))
|
|
{
|
|
rb.AddForce(Vector3.up * jump);
|
|
}
|
|
drive = Mathf.SmoothDamp(drive, tfrc, ref vel, delay);
|
|
Debug.DrawLine(base.transform.position, nps);
|
|
}
|
|
|
|
public void Position()
|
|
{
|
|
if (!path || !rb || !connected)
|
|
{
|
|
return;
|
|
}
|
|
Vector3 position = rb.position;
|
|
Vector3 tangent = Vector3.zero;
|
|
int kn = 0;
|
|
Vector3 zero = Vector3.zero;
|
|
zero = (nps = ((!usealpha) ? path.FindNearestPointWorldXZ(position, 15, ref kn, ref tangent, ref alpha) : path.transform.TransformPoint(path.InterpCurve3D(curve, alpha, true))));
|
|
zero.y = position.y;
|
|
Vector3 vector = zero - position;
|
|
vector.y = 0f;
|
|
Vector3 force = vector * impulse;
|
|
rb.AddForce(force, ForceMode.Impulse);
|
|
zero.y = position.y;
|
|
rb.MovePosition(zero);
|
|
base.transform.position = zero;
|
|
Vector3 vector2 = path.transform.TransformPoint(path.InterpCurve3D(curve, alpha + 0.0001f, true));
|
|
vector2.y = position.y;
|
|
if (align)
|
|
{
|
|
Vector3 normalized = (vector2 - zero).normalized;
|
|
Vector3 forward = base.transform.forward;
|
|
forward.y = 0f;
|
|
forward = forward.normalized;
|
|
float num = Vector3.Angle(forward, normalized);
|
|
if (Vector3.Cross(forward, normalized).y < 0f)
|
|
{
|
|
num = 0f - num;
|
|
}
|
|
Quaternion rotation = rb.rotation;
|
|
Quaternion quaternion = Quaternion.Euler(new Vector3(0f, num, 0f));
|
|
rb.MoveRotation(rotation * quaternion);
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (!path || !rb || !connected)
|
|
{
|
|
return;
|
|
}
|
|
Vector3 position = rb.position;
|
|
Vector3 tangent = Vector3.zero;
|
|
int kn = 0;
|
|
Vector3 zero = Vector3.zero;
|
|
zero = (nps = ((!usealpha) ? path.FindNearestPointWorldXZ(position, 15, ref kn, ref tangent, ref alpha) : path.transform.TransformPoint(path.InterpCurve3D(curve, alpha, true))));
|
|
zero.y = position.y;
|
|
Vector3 vector = zero - position;
|
|
vector.y = 0f;
|
|
Vector3 force = vector * impulse;
|
|
float num = force.magnitude / Time.fixedDeltaTime;
|
|
if (num > breakforce)
|
|
{
|
|
connected = false;
|
|
rb.angularVelocity = Vector3.zero;
|
|
}
|
|
if (!connected)
|
|
{
|
|
return;
|
|
}
|
|
rb.AddForce(force, ForceMode.Impulse);
|
|
zero.y = position.y;
|
|
rb.MovePosition(zero);
|
|
Vector3 vector2 = path.transform.TransformPoint(path.InterpCurve3D(curve, alpha + 0.0001f, true));
|
|
vector2.y = position.y;
|
|
if (align)
|
|
{
|
|
Vector3 normalized = (vector2 - zero).normalized;
|
|
Vector3 forward = base.transform.forward;
|
|
forward.y = 0f;
|
|
forward = forward.normalized;
|
|
float num2 = Vector3.Angle(forward, normalized);
|
|
if (Vector3.Cross(forward, normalized).y < 0f)
|
|
{
|
|
num2 = 0f - num2;
|
|
}
|
|
Quaternion rotation = rb.rotation;
|
|
Quaternion quaternion = Quaternion.Euler(new Vector3(0f, num2, 0f));
|
|
rb.MoveRotation(rotation * quaternion);
|
|
}
|
|
if (drag != 0f)
|
|
{
|
|
rb.AddForce(-rb.velocity * drag);
|
|
}
|
|
if (drive != 0f)
|
|
{
|
|
rb.AddForce((zero - vector2).normalized * drive, ForceMode.Force);
|
|
}
|
|
}
|
|
}
|