-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBallTest.cs
46 lines (41 loc) · 1.21 KB
/
BallTest.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
using UnityEngine;
using System.Collections;
public class BallTest : MonoBehaviour {
GameObject vehicle;
Vector3 goal;
float slowing = 1;
float acc = 2;
Vector3 vel;
Vector3 pos;
// Use this for initialization
void Start () {
vehicle = Instantiate(
Resources.Load("GameObjects/SphericalVehicle") as GameObject
) as GameObject;
vehicle.transform.localScale = new Vector3(1,1,1);
pos = Vector3.zero;
vehicle.transform.position = pos;
vehicle.transform.parent = transform;
vehicle.SetActive(true);
goal = new Vector3(15, 0, 5);
vel = new Vector3(0,0,10);
}
// Update is called once per frame
void Update () {
pos = vehicle.transform.position;
float dist = Vector3.Distance(pos, goal);
slowing = Mathf.Max(0.5f * vel.magnitude * vel.magnitude / acc, 3);
Vector3 desired = (goal - pos).normalized * (vel.magnitude + acc*Time.deltaTime);
if (dist < slowing) {
desired /= slowing;
}
Vector3 steer = desired - vel;
vel += Mathf.Min(acc*Time.deltaTime, steer.magnitude) * steer.normalized;
vehicle.transform.Translate(vel * Time.deltaTime, Space.World);
print(dist + " " + vel.magnitude);
}
void OnDrawGizmos() {
Gizmos.color = Color.blue;
Gizmos.DrawSphere(goal, 0.4f);
}
}