-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gravity.cs
77 lines (51 loc) · 1.16 KB
/
Gravity.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gravity : MonoBehaviour
{
const float G = 667.4f;
public static List<Gravity> Attractors;
public Rigidbody rb;
public bool inGravity = false;
private Rigidbody rbToAttract;
private GameObject ball;
void FixedUpdate()
{
if (inGravity)
{
//rbToAttract.useGravity = false;
Vector3 direction = rb.position - rbToAttract.position;
float distance = direction.magnitude;
if (distance == 0f)
{
return;
}
float forceMagnitude = G * (rb.mass * rbToAttract.mass) / Mathf.Pow(distance, 2);
Vector3 force = direction.normalized * forceMagnitude;
rbToAttract.useGravity = false;
rbToAttract.AddForce(force);
}
}
void OnEnable()
{
if (Attractors == null)
Attractors = new List<Gravity>();
rb.mass = 0.0001f;
Attractors.Add(this);
Debug.Log(this);
}
void OnDisable()
{
Attractors.Remove(this);
}
void OnTriggerEnter(Collider other)
{
if (other.name == "Ball")
{
rb.mass = 750;
rbToAttract = other.gameObject.GetComponent<Rigidbody>();
inGravity = true;
//rbToAttract.useGravity = true;
}
}
}