Commit b3fd9bc4 authored by Martin Mihálik's avatar Martin Mihálik 🔥
Browse files

Meteor Spawning

parent 4867336f
......@@ -11,10 +11,10 @@ GameObject:
- component: {fileID: 2654140422281934951}
- component: {fileID: 2654140422281934944}
- component: {fileID: 2654140422281934947}
- component: {fileID: 2654140422281934946}
- component: {fileID: 4295960864059702635}
- component: {fileID: 2654140422281934945}
- component: {fileID: 2654140422281934950}
m_Layer: 3
m_Layer: 11
m_Name: Wall
m_TagString: Untagged
m_Icon: {fileID: 0}
......@@ -98,7 +98,7 @@ BoxCollider2D:
m_Material: {fileID: 0}
m_IsTrigger: 0
m_UsedByEffector: 0
m_UsedByComposite: 0
m_UsedByComposite: 1
m_Offset: {x: 0, y: 0}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
......@@ -112,7 +112,7 @@ BoxCollider2D:
serializedVersion: 2
m_Size: {x: 1, y: 1}
m_EdgeRadius: 0
--- !u!50 &2654140422281934946
--- !u!50 &4295960864059702635
Rigidbody2D:
serializedVersion: 4
m_ObjectHideFlags: 0
......@@ -128,7 +128,7 @@ Rigidbody2D:
m_LinearDrag: 0
m_AngularDrag: 0.05
m_GravityScale: 1
m_Material: {fileID: 6200000, guid: 0af07f9debd10a04cbda18bd1aeb291a, type: 2}
m_Material: {fileID: 0}
m_Interpolate: 0
m_SleepingMode: 1
m_CollisionDetection: 0
......
......@@ -2,35 +2,74 @@ using UnityEngine;
public class MeteorSpawner : MonoBehaviour
{
[SerializeField] private Transform m_Player;
[SerializeField] private Transform m_Source;
[SerializeField] private Transform m_Target;
[SerializeField] private Transform m_Parent;
[SerializeField] private GameObject m_Prefab;
private Transform m_Parent;
void Start()
private bool m_Running;
[SerializeField] private float m_MeteorSpeed;
[SerializeField] private float m_SpawnRate;
[SerializeField] private float m_SpawnRateGrowthRate;
public float MeteorSpeed { get; set; }
private float SpawnPeriod { get => 1 / m_SpawnRate; }
private void Start()
{
m_Parent = GetComponent<Transform>();
Resume();
}
void Update()
private void Update()
{
var startingPoint = new Vector2(0, 10);
var endingPoint = new Vector2(m_Player.position.x, m_Player.position.y) - startingPoint;
m_SpawnRate += m_SpawnRateGrowthRate * Time.deltaTime;
}
if (Input.GetKeyDown(KeyCode.M))
private void SpawnMeteors()
{
if (!m_Running)
{
SpawnMeteor(startingPoint, endingPoint, 10);
return;
}
var sourcePoint = RandomPointInTransform(m_Source);
var targetPoint = RandomPointInTransform(m_Target);
SpawnMeteor(sourcePoint, targetPoint, m_MeteorSpeed, m_Parent);
Invoke("SpawnMeteors", SpawnPeriod);
}
private void SpawnMeteor(Vector2 startingPosition, Vector2 direction, float speed)
private void SpawnMeteor(Vector2 source, Vector2 target, float speed, Transform parent)
{
Vector3 startingPosition3D = startingPosition;
Vector3 startingPosition3D = source;
var meteor = Instantiate(m_Prefab, startingPosition3D, Quaternion.identity, m_Parent);
var meteor = Instantiate(m_Prefab, startingPosition3D, Quaternion.identity, parent);
var meteorController = meteor.GetComponent<MeteorController>();
meteorController.m_Direction = direction;
meteorController.m_Direction = (target - source).normalized;
meteorController.m_Speed = speed;
}
static private Vector2 RandomPointInTransform(Transform transform)
{
return new Vector2(
Random.Range(transform.position.x - transform.localScale.x / 2, transform.position.x + transform.localScale.x / 2),
Random.Range(transform.position.y - transform.localScale.y / 2, transform.position.y + transform.localScale.y / 2)
);
}
public void Resume()
{
m_Running = true;
SpawnMeteors();
}
public void Pause()
{
m_Running = false;
}
}
......@@ -53,4 +53,4 @@ Physics2DSettings:
m_ColliderAsleepColor: {r: 0.5686275, g: 0.95686275, b: 0.54509807, a: 0.36078432}
m_ColliderContactColor: {r: 1, g: 0, b: 1, a: 0.6862745}
m_ColliderAABBColor: {r: 1, g: 1, b: 0, a: 0.2509804}
m_LayerCollisionMatrix: ffffffff01f8ffff01f8ffffc1feffff01f8ffff01f8ffff09f8ffff09ffffff81f8ffff89f8ffff89f8ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
m_LayerCollisionMatrix: ffffffff01f0ffff01f0ffffc1f6ffff01f0ffff01f0ffff09f8ffff09ffffff81f0ffff89f8ffff89f8ffffc1f6ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
......@@ -16,7 +16,7 @@ TagManager:
- Collectible
- Meteor
- Explosion
-
- Wall
-
-
-
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment