Стреляйте пулей в направлении последней позиции игрока - PullRequest
0 голосов
/ 28 декабря 2018

Я пытаюсь закодировать беспилотник, который стреляет лазерной пулей до последней позиции игрока.Пуля должна также повернуться в положение игроков.

У меня есть следующий сценарий, но по какой-то причине он не стреляет.

using System.Collections.Generic;
using UnityEngine;

public class DroneShoot: MonoBehaviour
{

    public Transform bulletspawn;
    public Rigidbody2D bulletPrefab;
    public float bulletSpeed = 750;
    public float bulletDelay;

    private Transform player;
    private Rigidbody2D clone;

    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("spieler").GetComponent<Transform>();
    StartCoroutine(Attack());
    }

    IEnumerator Attack()
    {
        yield return new WaitForSeconds(bulletDelay);
        if (Vector3.Distance(player.transform.position, bulletspawn.transform.position) < 20)
        {
            Quaternion rotation = Quaternion.LookRotation(player.transform.position, bulletspawn.transform.position);
            bulletspawn.rotation = rotation;
            clone = Instantiate(bulletPrefab, bulletspawn.position, bulletspawn.rotation);

            clone.AddForce(bulletspawn.transform.right * bulletSpeed);
            StartCoroutine(Attack());
        }
    }
}

Ответы [ 2 ]

0 голосов
/ 28 декабря 2018

У меня есть решение.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DroneShoot: MonoBehaviour
{

public Transform bulletspawn;
public Rigidbody2D bulletPrefab;
public float bulletSpeed = 750;
public float bulletDelay;

private Transform player;
private Rigidbody2D clone;

// Use this for initialization
void Start()
{
    player = GameObject.FindGameObjectWithTag("spieler").GetComponent<Transform>();
    StartCoroutine(Attack());
}

IEnumerator Attack()
{
    yield return new WaitForSeconds(bulletDelay);
    if (Vector3.Distance(player.transform.position, bulletspawn.transform.position) < 20)
    {

        Vector3 difference = player.position - bulletspawn.position;
        float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        bulletspawn.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);

        clone = Instantiate(bulletPrefab, bulletspawn.position, bulletspawn.rotation);

        clone.AddForce(bulletspawn.transform.right * bulletSpeed);
        StartCoroutine(Attack());
    }
}
}
0 голосов
/ 28 декабря 2018

Похоже, что он попытается снова выстрелить, только если сможет выстрелить в первый раз, потому что StartCoroutine находится внутри проверки расстояния.

Я думаю, что он должен продолжать пытаться стрелять вечно.

IEnumerator Attack()
{
    while(true){
        yield return new WaitForSeconds(shootDelay);
        if (Vector3.Distance(player.transform.position, bulletspawn.transform.position) < 20)
        {
            Quaternion rotation = Quaternion.LookRotation(player.transform.position);
            bulletspawn.rotation = rotation;
            clone = Instantiate(bulletPrefab, bulletspawn.position, bulletspawn.rotation);

            clone.AddForce(bulletspawn.transform.right * bulletSpeed);
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...