参数的引用类型

    public GameObject targetGameObject;

    [Tooltip("移动速度")]
    public float speed = 0.02f;

    private void Awake()
    {
        Application.targetFrameRate = 60;
    }

    // Start is called before the first frame update
    void Start()
    {
        gameObject.transform.LookAt(targetGameObject.transform);
    }

    // Update is called once per frame
    void Update()
    {
        var p1 = targetGameObject.transform.position;
        var p2 = gameObject.transform.position;

        var p = p2 - p1;
       //返回向量的长度,也就是点P(x,y,z)到原点(0,0,0)的距离。 最常用的是用来返回物体的移动速度
        var distance = p.magnitude;

        if (distance > 0.3f)
        {
            gameObject.transform.Translate(0, 0, speed, Space.Self);
        }
        else
        {
            var objName = targetGameObject.name;
            var str = objName.Replace("红旗", "");
            var index = Convert.ToInt32(str);
            index = index + 1;
            index = index == 5 ? 1 : index;
            targetGameObject = GameObject.Find("红旗" + index);

            gameObject.transform.LookAt(targetGameObject.transform);
        }
    }
posted @ 2024-07-14 20:50  暖暖De幸福  阅读(2)  评论(0编辑  收藏  举报