-
Notifications
You must be signed in to change notification settings - Fork 0
/
BeamEmitter.cs
71 lines (54 loc) · 1.52 KB
/
BeamEmitter.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
using UnityEngine;
using System.Collections;
public class BeamEmitter : MonoBehaviour {
public float rayLength;
public float forceStr;
public float platePull;
public bool success = false;
public GameObject powerPlate;
RaycastHit hit;
Rigidbody rb;
MeshRenderer plateRender;
void Start ()
{
success = false;
rb = GetComponent<Rigidbody>();
rb.AddTorque(-Vector3.up * rb.mass);
}
void FixedUpdate()
{
// Beam
if (Physics.Raycast(transform.position + transform.forward, // avoids the emit pad collider
transform.forward * rayLength,
out hit))
{
/// light powerplate
if (hit.collider.gameObject == powerPlate)
{
plateRender = powerPlate.GetComponent<MeshRenderer>();
plateRender.material.SetColor("_EmissionColor", Color.grey);
success = true;
}
/// push asteroids
else if (hit.rigidbody)
{
Rigidbody hitRb = hit.rigidbody;
Vector3 beamPush = hit.transform.position - transform.position;
hitRb.AddForceAtPosition(beamPush.normalized * (forceStr / hit.distance),
hit.point);
}
}
if (success)
{
/// stick to plate
rb.angularVelocity = Vector3.Lerp(rb.angularVelocity,
Vector3.zero,
Time.deltaTime * platePull * 2.2f);
transform.rotation =
Quaternion.Slerp
(transform.rotation,
Quaternion.LookRotation(powerPlate.transform.position - transform.position, transform.up),
Time.deltaTime * platePull);
}
}//Update
}