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  
  • Home
  •  > 
  • 2024年

page.phpテンプレートを利用しています。

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);
        }
    }
}

Bullet


public class Bullet : MonoBehaviour
{
    Rigidbody2D  rbody; 
     public float speed;   
    void Start()
    {
      rbody = this.GetComponent();
     //  playerRb = GameObject.Find("Player").GetComponent();
    }
     void FixedUpdate()
    {
        rbody.velocity = new Vector2(speed, 0);    
    }
    void Update()
    {
    //   rbody.velocity = new Vector2(speed, 0);
     //  transform.position += new Vector3(-15f * Time.deltaTime, 0, 0);
     //  transform.Rotate(0f, 180f, 0f);
     //    transform.position += new Vector3(15f * Time.deltaTime, 0,  0);
     if(transform.position.x > 20.0f )
        {    
             Destroy(gameObject);
         }
      }
    }

Player


public class Player : MonoBehaviour
{
    Rigidbody2D rbody;              //Rigidbody2D型の変数
    float axisH = 0.0f;             //入力
    public float speed = 3.0f;
    public FixedJoystick joystick;  //InspectorからJoystickを取得 
    // Start is called before the first frame update
    void Start()
    {
        //Rigidbody2Dを取ってくる
        rbody = this.GetComponent();
    }

    // Update is called once per frame
     void Update()
    {
      //  axisH = Input.GetAxisRaw("Horizontal");
        axisH = joystick.Horizontal;
        //向きの調整
        if (axisH > 0.0f)
        {
            //右移動
            Debug.Log("右移動");
            transform.localScale = new Vector2(1, 1);
        }
        else if (axisH < 0.0f)
        {
            Debug.Log("左移動");
            transform.localScale = new Vector2(-1, 1);
        }
    }
    void FixedUpdate()
    {
        //速度を更新する
        rbody.velocity = new Vector2(axisH * speed, rbody.velocity.y);
    }
}