2025.03.09 unity  
 2025.03.09 unity  
 2025.03.09 unity  
 2025.03.09 unity  
 2025.03.08 unity  
 2024.11.30 unity  
 2024.11.30 unity  
 2024.11.30 未分類  

Interface2


public interface IDamageTest
{
   public void Damage(int damage);
    public void Death();
    public void DecrementLife();
    public void DecrementDed();
}
//enemyに
using UnityEngine;
public class EnemyController : MonoBehaviour, IDamageTest
{
    public float speed = 2.0f;     // 移動速度
    public bool isToRight = false; // true=右向き false=左向き
    public float revTime = 0;   // 反転時間  
    float time = 0;
   public GameObject BulletLeft;
   public GameObject BulletRight;
    public Transform bulletPoint;
 public void Damage(int damage)
   {
   }
  public void Death()
    {
      gameObject.SetActive(false);
        Invoke(nameof(DelayMethod), 3.5f);
     //   Debug.Log("Enemyを倒した");
    }
    void DelayMethod()
    {
       gameObject.SetActive(true);
    }

    public void DecrementLife()
    {
    }
    public void DecrementDed()
    {
    }
    // Start is called before the first frame update
    void Start()
    {
     if (isToRight)
     {
    //transform.localScale = new Vector2(-1, 1);// 向きの変更
     transform.eulerAngles = new Vector3(0, 0, 0);
     }
 }
    // Update is called once per frame
 void Update()
 {
    if(revTime > 0)
 {
 time += Time.deltaTime;
    if (time >= revTime)
{
   isToRight = !isToRight;     //フラグを反転させる
    time = 0;                   //タイマーを初期化
    if (isToRight)
 {
 //   transform.localScale = new Vector2(-1, 1);  // 向きの変更
     transform.eulerAngles = new Vector3(0, 180, 0);
     Instantiate(BulletRight, bulletPoint.position, Quaternion.Euler(0,180,0));                  
   }
   else
   {
 //  transform.localScale = new Vector2(1, 1);   // 向きの変更
    transform.eulerAngles = new Vector3(0, 0, 0);       
    Instantiate(BulletLeft, bulletPoint.position, Quaternion.Euler(0,0,0));
    }
   }
 }
 }
void FixedUpdate()
 {
    // 速度を更新する
  // Rigidbody2D を取ってくる
   Rigidbody2D rbody = GetComponent();
   if (isToRight)
 {
   rbody.linearVelocity = new Vector2(speed, rbody.linearVelocity.y);
  }
   else
  {
   rbody.linearVelocity = new Vector2(-speed, rbody.linearVelocity.y);
   }      
  } 
}

////////////
//enemy BulletLeftに
using UnityEngine;
public class BulletLeft : MonoBehaviour
{
     Rigidbody2D  rbody; 
     public float speed;   
    void Start()
    {
      rbody = this.GetComponent();
    }
     void FixedUpdate()
    {
       if (rbody!= null)
     {
     rbody.linearVelocity = new Vector2(-speed, 0); 
     }
       Invoke(nameof(DelayMethod), 2.1f);
    }
 void DelayMethod()
    {
       rbody.linearVelocity = Vector2.zero; 
    }
     void Update()
    {
     if (transform.position.x < -40.0f)
     {
        Destroy(gameObject);
     }
    }
 private void OnTriggerEnter2D(Collider2D coll)
    {  
    var damagetest = coll.gameObject.GetComponent();
  if (coll.gameObject.CompareTag("Player"))
     {
   if (damagetest != null) { 
     //   Destroy(gameObject);
   damagetest.DecrementLife();
   }
     }
    }
 }
//////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletRight : MonoBehaviour
{
     Rigidbody2D  rbody; 
     public float speed;   
 void Start()
    {
      rbody = this.GetComponent();
      //  transform.position += new Vector3(10f * Time.deltaTime, 0, 0);
    }
    void FixedUpdate()
    {
    if (rbody!= null)
    {
  rbody.linearVelocity = new Vector2(speed, 0); 
    }
     Invoke(nameof(DelayMethod), 2.1f);
    }
    void DelayMethod()
    {
       rbody.linearVelocity = Vector2.zero; 
    }
  void Update()
    {
       // transform.position += new Vector3(10f * Time.deltaTime, 0, 0);
      //  transform.Translate(0, 0.03f, 0);
    if (transform.position.x > 36.0f)
   {
      Destroy(gameObject);
     }
    }
  private void OnTriggerEnter2D(Collider2D coll)
    {  
    var damagetest = coll.gameObject.GetComponent();
    if (coll.gameObject.CompareTag("Player"))
       {
     if (damagetest != null) { 
      //   Destroy(gameObject);
      damagetest.DecrementLife();
       }
     }
    }
  }
  

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です