70 lines
1.1 KiB
C#
70 lines
1.1 KiB
C#
using UnityEngine;
|
|
|
|
[ExecuteInEditMode]
|
|
public class BallBounce : MonoBehaviour
|
|
{
|
|
public float ground;
|
|
|
|
public float radius = 1f;
|
|
|
|
public float vel;
|
|
|
|
public float spring = 10f;
|
|
|
|
public float py = 1f;
|
|
|
|
public float mass = 1f;
|
|
|
|
public float timescale = 1f;
|
|
|
|
private MegaStretch mod;
|
|
|
|
private MegaModifyObject modobj;
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
float num = Time.fixedDeltaTime * timescale;
|
|
float num2 = -9.81f * num;
|
|
if (py < ground)
|
|
{
|
|
num2 += spring * (ground - py) / mass;
|
|
}
|
|
vel += num2 * num;
|
|
py += vel * num;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!mod)
|
|
{
|
|
mod = GetComponent<MegaStretch>();
|
|
modobj = GetComponent<MegaModifyObject>();
|
|
}
|
|
if ((bool)mod)
|
|
{
|
|
Vector3 position = base.transform.position;
|
|
float num = py - ground;
|
|
if (num > 0f)
|
|
{
|
|
num = 0f;
|
|
}
|
|
float num2 = py;
|
|
if (num2 < ground)
|
|
{
|
|
num2 = ground;
|
|
}
|
|
if (mod.amount == 0f && num == 0f)
|
|
{
|
|
modobj.enabled = false;
|
|
}
|
|
else
|
|
{
|
|
modobj.enabled = true;
|
|
}
|
|
mod.amount = num / radius;
|
|
position.y = num2;
|
|
base.transform.position = position;
|
|
}
|
|
}
|
|
}
|