-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathMovement.cs
136 lines (112 loc) · 4.52 KB
/
Movement.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Copyright (C) Luaek - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Maxi Levi <maxilevi@live.com>, November 2017
*/
using UnityEngine;
using System.Collections;
using Assets;
public class Movement : MonoBehaviour {
public float Speed = 16;
public float TurnSpeed = 3f;
public TrailRenderer LeftTrail, RightTrail;
public Color TrailColor;
public Vector3 LeftPosition, RightPosition;
public Material TrailMaterial;
public AudioSource LeftSource, RightSource;
public AudioClip SwooshClip;
private GameObject Debris;
private bool _lock;
private float _leftTargetVolume = 1, _rightTargetVolume = 1;
private float _originalVolume;
private float _speed = 0;
void Start(){
Debris = GameObject.FindGameObjectWithTag ("Debris");
LeftSource = GameObject.FindGameObjectWithTag ("LeftSource").GetComponent<AudioSource>();
RightSource = GameObject.FindGameObjectWithTag ("RightSource").GetComponent<AudioSource>();
_originalVolume = (RightSource.volume + LeftSource.volume) * .5f;
}
public void Lock(){
_lock = true;
}
public void Unlock(){
_lock = false;
}
// Update is called once per frame
void Update () {
LeftSource.volume = Mathf.Lerp (LeftSource.volume, _originalVolume * _leftTargetVolume, Time.deltaTime * 2f);
RightSource.volume = Mathf.Lerp (RightSource.volume, _originalVolume * _rightTargetVolume, Time.deltaTime * 2f);
if (LeftSource.volume < 0.05f)
LeftSource.Stop ();
if (RightSource.volume < 0.05f)
RightSource.Stop ();
if (_lock)
return;
_speed = Mathf.Lerp (_speed, Speed, Time.deltaTime * .25f);
transform.parent.position += transform.forward * Time.deltaTime * 4 * _speed;
transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.Euler(Vector3.zero), Time.deltaTime * 2.5f);
float zAngle = transform.localRotation.eulerAngles.z;
float xAngle = transform.localRotation.eulerAngles.x;
if (zAngle > 45 && zAngle < 135 || zAngle > 225 && zAngle < 315 || xAngle > 45 && xAngle < 90 || xAngle > 270 && xAngle < 315) {
StartTrail (ref LeftTrail, LeftPosition);
LeftSource.transform.position = LeftPosition;
LeftSource.clip = SwooshClip;
_leftTargetVolume = 1;
if(!LeftSource.isPlaying)
LeftSource.Play ();
} else {
StopTrail (ref LeftTrail);
_leftTargetVolume = 0;
}
if (zAngle > 45 && zAngle < 135 || zAngle > 225 && zAngle < 315 || xAngle > 45 && xAngle < 90 || xAngle > 270 && xAngle < 315) {
StartTrail (ref RightTrail, RightPosition);
RightSource.transform.position = RightPosition;
RightSource.clip = SwooshClip;
_rightTargetVolume = 1;
if(!RightSource.isPlaying)
RightSource.Play ();
} else {
StopTrail (ref RightTrail);
_rightTargetVolume = 0;
}
float scale = (Time.timeScale != 1) ? (1 / Time.timeScale) * .5f : 1;
KeyCode UpKey = (Options.Invert) ? KeyCode.W : KeyCode.S;
KeyCode DownKey = (Options.Invert) ? KeyCode.S : KeyCode.W;
if (Input.GetKey(UpKey))
{
transform.localRotation = Quaternion.Euler(transform.localRotation.eulerAngles + Vector3.right * Time.deltaTime * 64f * TurnSpeed * scale);
}
if (Input.GetKey(DownKey))
{
transform.localRotation = Quaternion.Euler(transform.localRotation.eulerAngles - Vector3.right * Time.deltaTime * 64f * TurnSpeed * scale);
}
if (Input.GetKey (KeyCode.A)) {
transform.localRotation = Quaternion.Euler(transform.localRotation.eulerAngles + Vector3.forward * Time.deltaTime * 64f * TurnSpeed * scale);
transform.parent.Rotate(- Vector3.up * Time.deltaTime * 64f * TurnSpeed * scale);
}
if (Input.GetKey (KeyCode.D)) {
transform.localRotation = Quaternion.Euler(transform.localRotation.eulerAngles - Vector3.forward * Time.deltaTime * 64f * TurnSpeed * scale);
transform.parent.Rotate( Vector3.up * Time.deltaTime * 64f * TurnSpeed * scale );
}
}
void StartTrail(ref TrailRenderer Trail, Vector3 Position){
if (Trail != null)
return;
GameObject go = new GameObject ("Trail");
go.transform.parent = this.gameObject.transform;
Trail = go.AddComponent<TrailRenderer> ();
Trail.widthMultiplier = .25f;
Trail.endColor = new Color (0, 0, 0, 0);
Trail.startColor = TrailColor;
Trail.transform.localPosition = Position;
Trail.material = TrailMaterial;
Trail.time = 1.5f;
}
void StopTrail(ref TrailRenderer Trail){
if (Trail == null)
return;
Trail.transform.parent = (Debris != null) ? Debris.transform : null;
Destroy (Trail.gameObject, Trail.time+1);
Trail = null;
}
}