Unity Project
This commit is contained in:
80
Assets/_Content/Scripts/Rocket.cs
Normal file
80
Assets/_Content/Scripts/Rocket.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.WSA;
|
||||
|
||||
public class Rocket : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public class References
|
||||
{
|
||||
public Rigidbody Rigidbody;
|
||||
public ParticleSystem Fire;
|
||||
public GameObject Explosion;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public bool LaunchOnEnable = false;
|
||||
public float Speed = 20;
|
||||
public float Duration = 5;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class State
|
||||
{
|
||||
public bool Launched = false;
|
||||
public bool Disabled = false;
|
||||
public float FlightTime = 0;
|
||||
}
|
||||
|
||||
[SerializeField] private References _references;
|
||||
[SerializeField] private Settings _settings;
|
||||
[SerializeField] private State _state;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (_settings.LaunchOnEnable)
|
||||
{
|
||||
Launch();
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_state.Launched && _state.FlightTime < _settings.Duration)
|
||||
{
|
||||
Vector3 force = transform.up * _settings.Speed * Time.deltaTime * 100;
|
||||
|
||||
_references.Rigidbody.AddForce(force);
|
||||
|
||||
_state.FlightTime += Time.deltaTime;
|
||||
}
|
||||
else if (!_state.Disabled)
|
||||
{
|
||||
_state.Disabled = true;
|
||||
|
||||
_references.Fire.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
void OnCollisionEnter(Collision col)
|
||||
{
|
||||
if (_state.Launched && _state.FlightTime > .5f)
|
||||
{
|
||||
GameObject explosion = Instantiate(_references.Explosion);
|
||||
explosion.transform.position = transform.position;
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
[ContextMenu("Launch")]
|
||||
public void Launch()
|
||||
{
|
||||
_state.Launched = true;
|
||||
_state.Disabled = false;
|
||||
_state.FlightTime = 0;
|
||||
|
||||
_references.Fire.Play();
|
||||
}
|
||||
}
|
||||
2
Assets/_Content/Scripts/Rocket.cs.meta
Normal file
2
Assets/_Content/Scripts/Rocket.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d3675883269c63a409e9c4817365893c
|
||||
36
Assets/_Content/Scripts/RocketExplosion.cs
Normal file
36
Assets/_Content/Scripts/RocketExplosion.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class RocketExplosion : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public class References
|
||||
{
|
||||
public SphereCollider Collider;
|
||||
public ParticleSystem Explosion;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public float Radius = 5;
|
||||
}
|
||||
|
||||
[SerializeField] private References _references;
|
||||
[SerializeField] private Settings _settings;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_references.Collider.radius = 1;
|
||||
_references.Explosion.Play();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
_references.Collider.radius += Time.deltaTime * 15;
|
||||
|
||||
if (_references.Collider.radius > _settings.Radius)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/_Content/Scripts/RocketExplosion.cs.meta
Normal file
2
Assets/_Content/Scripts/RocketExplosion.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fd0392a39cb263e4ca86378ad9d3f120
|
||||
Reference in New Issue
Block a user