Files
Unity2DProject/Assets/UnityTechnologies/Final/Scripts/Final/Projectile.cs
Mingu Kim 20c7e2ba3a 2DProject 및 Sprite 생성
1. 2DProject 생성
2. Sprite 및 AnimationController
2025-01-25 00:43:18 +09:00

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);
}
}