-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCometLauncher.cs
36 lines (26 loc) · 1.08 KB
/
CometLauncher.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CometLauncher : MonoBehaviour
{
[Header("Specify the fixed star(ex: sun)")]
public GameObject FixedStar;
[Range(1,1.414f)]
public float COMET_COEFFICIENT;
[Space(25)]
public UnivarsalGravitationController UG_Director;
void Start() {
Vector3 myVec = transform.position;
Vector3 CentralVec = FixedStar.transform.position;
// Calculate launch velocity
var G = UG_Director.CONSTANT * UG_Director.COEFFICIENT;
var M = FixedStar.GetComponent<Rigidbody>().mass;
var r = Vector3.Distance(myVec, CentralVec);
// Calculate launch velocity
// Launch in a direction normal to the vector between the two points
float initSpeed = (float)System.Math.Sqrt(G * M / r);
Vector3 initVelocity =
Quaternion.Euler(0, 90, 0) * (CentralVec - myVec).normalized * initSpeed * COMET_COEFFICIENT;
GetComponent<Rigidbody>().velocity = initVelocity;
}
}