mirror of
https://github.com/CoonS2/Unity2DProject.git
synced 2026-02-05 12:13:29 +09:00
41 lines
985 B
C#
41 lines
985 B
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// Handle the projectile launched by the player to fix the robots.
|
|
/// </summary>
|
|
public class Projectile : MonoBehaviour
|
|
{
|
|
Rigidbody2D rigidbody2d;
|
|
|
|
void Awake()
|
|
{
|
|
rigidbody2d = GetComponent<Rigidbody2D>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
//destroy the projectile when it reach a distance of 1000.0f from the origin
|
|
if(transform.position.magnitude > 1000.0f)
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
//called by the player controller after it instantiate a new projectile to launch it.
|
|
public void Launch(Vector2 direction, float force)
|
|
{
|
|
rigidbody2d.AddForce(direction * force);
|
|
}
|
|
|
|
void OnCollisionEnter2D(Collision2D other)
|
|
{
|
|
Enemy e = other.collider.GetComponent<Enemy>();
|
|
|
|
//if the object we touched wasn't an enemy, just destroy the projectile.
|
|
if (e != null)
|
|
{
|
|
e.Fix();
|
|
}
|
|
|
|
Destroy(gameObject);
|
|
}
|
|
}
|