走りながらFireBallを投げる

走りながら投げる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Buring : MonoBehaviour
{
private int num = 1;
public GameObject rayPoint;
public GameObject point_front;
public GameObject point_back;
private SpriteRenderer spriteRenderer;
// ★追加(Shot)
public GameObject fireBallPrefab;
private int speed = 10;
void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
rayPoint.transform.position = point_back.transform.position;
// ★追加(Shot)
StartCoroutine(Shot());
}
void Update()
{
transform.Translate(Vector2.left * Time.deltaTime * speed * num); // ★追加(Shot)変数のspeedにコードを変更
RaycastHit2D hit2d = Physics2D.Raycast(rayPoint.transform.position, Vector2.left * num, 0.2f);
Debug.DrawRay(rayPoint.transform.position, Vector3.left * num, Color.white, 0.2f);
if (hit2d.collider != null)
{
if (hit2d.collider.CompareTag("Floor"))
{
num *= -1;
}
}
if (num == 1)
{
spriteRenderer.flipX = false;
rayPoint.transform.position = point_back.transform.position;
}
else if (num == -1)
{
spriteRenderer.flipX = true;
rayPoint.transform.position = point_front.transform.position;
}
}
// ★追加(Shot)
private IEnumerator Shot()
{
while(true)
{
yield return new WaitForSeconds(6); // 走る(6秒間)
speed = 0; // 停止
yield return new WaitForSeconds(1);
// ForeBallを5発発射
for (int i = 0; i < 5; i++)
{
GameObject fireBall = Instantiate(fireBallPrefab, transform.position, Quaternion.identity);
Rigidbody2D fireBallRb2d = fireBall.GetComponent<Rigidbody2D>();
Destroy(fireBall, 8.0f);
int x = Random.Range(-1, 2); // ランダム要素の追加
fireBallRb2d.AddForce(new Vector2(x, 1) * 500); // 投げる方向を散らす
yield return new WaitForSeconds(0.5f);
}
yield return new WaitForSeconds(1.5f);
speed = 10; // 走り再開
}
}
}


【2022版】DarkCastle(全39回)
他のコースを見る
走りながら投げる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Buring : MonoBehaviour
{
private int num = 1;
public GameObject rayPoint;
public GameObject point_front;
public GameObject point_back;
private SpriteRenderer spriteRenderer;
// ★追加(Shot)
public GameObject fireBallPrefab;
private int speed = 10;
void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
rayPoint.transform.position = point_back.transform.position;
// ★追加(Shot)
StartCoroutine(Shot());
}
void Update()
{
transform.Translate(Vector2.left * Time.deltaTime * speed * num); // ★追加(Shot)変数のspeedにコードを変更
RaycastHit2D hit2d = Physics2D.Raycast(rayPoint.transform.position, Vector2.left * num, 0.2f);
Debug.DrawRay(rayPoint.transform.position, Vector3.left * num, Color.white, 0.2f);
if (hit2d.collider != null)
{
if (hit2d.collider.CompareTag("Floor"))
{
num *= -1;
}
}
if (num == 1)
{
spriteRenderer.flipX = false;
rayPoint.transform.position = point_back.transform.position;
}
else if (num == -1)
{
spriteRenderer.flipX = true;
rayPoint.transform.position = point_front.transform.position;
}
}
// ★追加(Shot)
private IEnumerator Shot()
{
while(true)
{
yield return new WaitForSeconds(6); // 走る(6秒間)
speed = 0; // 停止
yield return new WaitForSeconds(1);
// ForeBallを5発発射
for (int i = 0; i < 5; i++)
{
GameObject fireBall = Instantiate(fireBallPrefab, transform.position, Quaternion.identity);
Rigidbody2D fireBallRb2d = fireBall.GetComponent<Rigidbody2D>();
Destroy(fireBall, 8.0f);
int x = Random.Range(-1, 2); // ランダム要素の追加
fireBallRb2d.AddForce(new Vector2(x, 1) * 500); // 投げる方向を散らす
yield return new WaitForSeconds(0.5f);
}
yield return new WaitForSeconds(1.5f);
speed = 10; // 走り再開
}
}
}


走りながらFireBallを投げる