Вот код:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
private Rigidbody2D rb;
private GameObject player;
public float damage;
public float speed;
private float timer;
public float timeToAttack = 4f;
void Start()
{
rb = GetComponent<Rigidbody2D>();
player = GameObject.FindGameObjectWithTag("Enemy");
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0f)
{
Attack();
}
}
private void Attack()
{
timer = timeToAttack;
Vector2 derection = player.transform.position - transform.position;
rb.velocity = new Vector2(derection.x, derection.y).normalized * speed;
float rot = Mathf.Atan2(-derection.x, -derection.y) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, rot);
}
}
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
private Rigidbody2D rb;
private GameObject player;
public float damage;
public float speed;
private float timer;
public float timeToAttack = 4f;
void Start()
{
rb = GetComponent<Rigidbody2D>();
player = GameObject.FindGameObjectWithTag("Enemy");
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0f)
{
Attack();
}
}
private void Attack()
{
timer = timeToAttack;
Vector2 derection = player.transform.position - transform.position;
rb.velocity = new Vector2(derection.x, derection.y).normalized * speed;
float rot = Mathf.Atan2(-derection.x, -derection.y) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, rot);
}
}
В итоге у меня просто появляется огромное количество пуль.