UnityProjekt/Assets/Scripts/RocketScript.cs

46 lines
1 KiB
C#
Raw Normal View History

2020-11-06 20:20:17 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RocketScript : MonoBehaviour
{
private float _fired;
private Rigidbody2D _rb;
private bool _goingRight;
// Start is called before the first frame update
void Start()
{
_rb = GetComponent<Rigidbody2D>();
}
private void OnEnable()
{
_fired = Time.fixedTime;
_goingRight = transform.localScale.x > 0;
}
// Update is called once per frame
void FixedUpdate()
{
if(!gameObject.activeSelf) return;
if (Time.fixedTime - _fired > 3)
{
gameObject.SetActive(false);
_rb.velocity = Vector2.zero;
}
_rb.AddForce(new Vector2(_goingRight ? 10 : -10, 0));
}
2020-11-06 21:30:54 +00:00
private void OnCollisionEnter2D(Collision2D other)
{
if (!other.gameObject.CompareTag("Enemy"))
return;
gameObject.SetActive(false);
_rb.velocity = Vector2.zero;
Destroy(other.gameObject);
}
2020-11-06 20:20:17 +00:00
}