0
点赞
收藏
分享

微信扫一扫

Unity关于不同脚本之间相互调用变量的方法

德州spark 2022-02-23 阅读 77

Unity关于不同脚本之间相互调用变量的方法_调用变量值

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class attack1 : MonoBehaviour

{

   public float wavespeed = 7;

   private GameObject Enemys;

   public Transform attack11;

   public GameObject weapons1;

   private float fireRate = 0.5f;

   private float nextFire;

   // Start is called before the first frame update

   void Start()

   {


   }


   // Update is called once per frame

   void Update()

   {

       Enemys = GameObject.FindGameObjectWithTag("Enemy");

       float diss = Enemys.GetComponent<Enemy>().dis;

       Vector3 dir= Enemys.GetComponent<Enemy>().offestPosition;

       Debug.Log(diss);

       if(diss<20)

       {

           float angle =

            Vector3.SignedAngle(Vector3.right, -dir, Vector3.forward);

           transform.eulerAngles =

                Vector3.Lerp(transform.eulerAngles, new Vector3(0, 0, angle), wavespeed);


       }

       if (diss < 10)

       {

           if (Time.time > nextFire)

           {

               nextFire = Time.time + fireRate;

               Instantiate(weapons1, attack11.position, attack11.rotation);

           }

       }

   }

}



using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class Enemy : MonoBehaviour

{

   public Transform Player;

   public Vector3 offestPosition;

   public float dis;

   // Start is called before the first frame update

   void Start()

   {


   }


   // Update is called once per frame

   void Update()

   {

       offestPosition = Player.position - transform.position;

       dis = (offestPosition).sqrMagnitude;

   }

}

举报

相关推荐

0 条评论