1
0
Fork 0
crew-of-one/Assets/Scripts/laser.cs
Ava Gaiety Wroten 0cc43d7fb3 Initial Commit
2020-07-30 13:17:01 -05:00

46 lines
962 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class laser : MonoBehaviour
{
public float destroyWhenY = 10.0f;
public float speed = 5.0f;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (!shouldDestroySelf()) moveUp();
}
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject.tag == "asteroid")
{
collider.gameObject.SendMessage("laserHit");
Destroy(gameObject);
}
}
bool shouldDestroySelf()
{
if (transform.position.y >= destroyWhenY)
{
Destroy(gameObject);
return true;
}
return false;
}
void moveUp()
{
Vector3 tempVect = new Vector3(0, 1, 0);
tempVect = tempVect * speed * Time.deltaTime;
rb.MovePosition(transform.position + tempVect);
}
}