知方号

知方号

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

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().dis;

       Vector3 dir= Enemys.GetComponent().offestPosition;

       Debug.Log(diss);

       if(diss 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;

   }

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。