blob: 516f9371d6c7244129ac5ed12ff83ab5f3617a45 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class RigidBodyCustomGravity : MonoBehaviour
{
Rigidbody body;
Renderer renderer;
float floatDelay = 0f;
void Awake()
{
body = GetComponent<Rigidbody>();
renderer = GetComponent<Renderer>();
body.useGravity = false;
}
void FixedUpdate()
{
if(body.IsSleeping())
{
renderer.material.SetColor(
"_BaseColor",
Color.gray
);
floatDelay = 0f;
return;
}
if(body.velocity.sqrMagnitude < 0.0005f)
{
// disable interpolation when ready to sleep
body.interpolation = RigidbodyInterpolation.None;
renderer.material.SetColor(
"_BaseColor",
Color.yellow
);
floatDelay += Time.deltaTime;
if(floatDelay >= 1f)
return;
}
else
floatDelay = 0f;
// enable interpolation when not sleeping
body.interpolation = RigidbodyInterpolation.Interpolate;
renderer.material.SetColor(
"_BaseColor",
Color.red
);
body.AddForce(
CustomGravity.GetGravity(body.position),
ForceMode.Acceleration
);
}
}
|