Unity-C#

 2024.07.07 unityC#  
 2024.05.15 unityC#  
 2024.05.15 unityC#  
 2022.11.06 unityC#  
 2022.11.06 unityC#  
 2022.11.06 unityC#  
 2022.11.06 unityC#  
 2022.11.06 unityC#  
 2022.08.16 spritekit  

EnemyController


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

public class EnemyController : MonoBehaviour
{
    // ヒットポイント
    public int hp = 3;
    //移動スピード
    public float speed = 0.5f;
    // 反応距離
    public float reactionDistance = 4.0f;
     float axisH;            //横軸値(-1.0 〜 0.0 〜 1.0)
    float axisV;            //縦軸値(-1.0 〜 0.0 〜 1.0)
    Rigidbody2D rbody;      //Rigidbody 2D

     Animator animator;  

   // bool isActive = false;      //アクティブフラグ

    public int arrangeId = 0;   //配置の識別に使う
    //  public GameObject player;

    // Use this for initialization
    void Start()
    {
        //Rigidbody2Dを得る
        rbody = GetComponent();
           animator = GetComponent();
    }

    // Update is called once per frame
    void Update()
    {
        //Playerのゲームオブジェクトを得る
        GameObject player = GameObject.FindGameObjectWithTag("Player");
        if (player != null)
        {
                //プレイヤーへの角度を求める
                float dx = player.transform.position.x - transform.position.x;
                float dy = player.transform.position.y - transform.position.y;
                float rad = Mathf.Atan2(dy, dx);
                float angle = rad * Mathf.Rad2Deg;
                //移動角度でアニメーションを変更する
                if (angle > -45.0f && angle <= 45.0f)
                {
                   animator.SetFloat("x", 1);
                  animator.SetFloat("y", 0);
                }
                else if (angle > 45.0f && angle <= 135.0f)
                {
              animator.SetFloat("x", 0);
             animator.SetFloat("y", 1);
                }
                else if (angle >= -135.0f && angle <= -45.0f)
                {
                 animator.SetFloat("x", 0);
             animator.SetFloat("y", -1);
                }
                else
                {
                  animator.SetFloat("x", -1);
             animator.SetFloat("y", 0);             
                }
                //移動するベクトルを作る
                axisH = Mathf.Cos(rad) * speed;
                axisV = Mathf.Sin(rad) * speed;
            }      
    }
     void FixedUpdate()
    {
            //移動
            rbody.velocity = new Vector2(axisH, axisV);
    }
private void OnTriggerEnter2D(Collider2D col)
    {
    if (col.gameObject.CompareTag("Player"))
        {
        //  Destroy(col.gameObject);
         gameObject.SetActive(false);
       //   Destroy(col.gameObject);
        }
    }
}