-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarvinEnemyMovement.cs
142 lines (119 loc) · 3.91 KB
/
MarvinEnemyMovement.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
137
138
139
140
141
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using Vector2 = UnityEngine.Vector2;
using Vector3 = UnityEngine.Vector3;
public class MarvinEnemyMovement : MonoBehaviour
{
[SerializeField] private bool Vertical = false;
[SerializeField] private bool Horizontal = false;
[SerializeField] private bool Sinus = false;
[SerializeField] private bool Cosinus = false;
[SerializeField] private float SpeedVertical;
[SerializeField] private float SpeedHorizontal;
[SerializeField] private float SpeedSinus;
[SerializeField] private float SpeedCosinus;
[SerializeField] private float timeVertical;
[SerializeField] private float timeHorizontal;
[SerializeField] private float timeSinus;
[SerializeField] private float timeCosinus;
[SerializeField] private bool DebugTest;
private float elapsedTimeVertical = 0f;
private float elapsedTimeHorizontal = 0f;
private float elapsedTimeSinus = 0f;
private float elapsedTimeCosinus = 0f;
public float radius = 1f;
public float speed = 2f;
private float angle;
private Vector3 center;
private Vector3 startPosition;
private Rigidbody2D rg;
// Start is called before the first frame update
void Start()
{
rg = gameObject.GetComponent<Rigidbody2D>();
rg.velocity = CalculateVelocity(); // Anfangsgeschwindigkeit setzen
center = transform.position;
startPosition = transform.position;
if (DebugTest)
{
elapsedTimeVertical = timeVertical / 2;
}
}
private void FixedUpdate()
{
rg.velocity = fieldsToVector();
//adjust time
if (elapsedTimeVertical >= timeVertical)
{
SpeedVertical = -1 * (SpeedVertical);
elapsedTimeVertical = 0f;
}
if (elapsedTimeHorizontal >= timeHorizontal)
{
SpeedHorizontal = -1 * (SpeedHorizontal);
elapsedTimeHorizontal = 0f;
}
if (elapsedTimeSinus >= timeSinus)
{
SpeedSinus = -1 * (SpeedSinus);
elapsedTimeSinus = 0f;
}
if (elapsedTimeCosinus >= timeCosinus)
{
SpeedCosinus = -1 * (SpeedCosinus);
elapsedTimeCosinus = 0f;
}
if (Vertical)
elapsedTimeVertical += Time.deltaTime;
if (Horizontal)
elapsedTimeHorizontal += Time.deltaTime;
if (Sinus)
elapsedTimeSinus += Time.deltaTime;
if (Cosinus)
elapsedTimeCosinus += Time.deltaTime;
}
// Update is called once per frame
void Update()
{
}
private Vector3 fieldsToVector()
{
Vector3 v = new Vector3();
if (Vertical)
{
v += (Vector3.up * Time.deltaTime * SpeedVertical);
}
if (Horizontal)
{
v += (Vector3.right * Time.deltaTime * SpeedHorizontal);
}
if (Sinus)
{
// Den Winkel basierend auf der Zeit und Geschwindigkeit ändern
angle += speed * Time.deltaTime;
// Neue Position des Objekts berechnen
float x = Mathf.Cos(angle) * radius;
float y = Mathf.Sin(angle) * radius;
Vector3 newPosition = center + new Vector3(x, y, 0f);
// Die Richtung und Geschwindigkeit der Bewegung aktualisieren
v += (newPosition - transform.position) / Time.deltaTime;
}
if (Cosinus)
{
//Implement cosinus movement here
}
return v;
}
private Vector2 CalculateVelocity()
{
// Startposition des Objekts berechnen
float x = Mathf.Cos(angle) * radius;
float y = Mathf.Sin(angle) * radius;
Vector3 startPos = center + new Vector3(x, y, 0f);
// Geschwindigkeit berechnen
return (startPos - transform.position) / Time.deltaTime;
}
}