用于射线投射的上电系统统一射速

时间:2018-04-04 04:57:03

标签: c# unity3d

我正在尝试实施一个与盒子碰撞的加电,然后提高你的火速。有人能告诉我什么是错的吗?

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

public class Powerup : MonoBehaviour {

    public float multiplier = 3f;
    public float duration = 10f;


    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            Pickup(other);
        }
    }
    IEnumerator Pickup(Collider player)
    {
        RaycastShooting.fireRate *= multiplier;
        Destroy(gameObject);
        yield return new WaitForSeconds(duration);

        RaycastShooting.fireRate = 4;


    }

}

提前致谢。这可能是一个小问题,我没有注意到,但这也是一个问题。

2 个答案:

答案 0 :(得分:1)

你没有使用Pickup()作为套路。 我认为你应该在这里使用Invoke()方法,因为它可能是你想要的:

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

public class Powerup : MonoBehaviour {

    public float multiplier = 3f;
    public float duration = 10f;


    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            RaycastShooting.fireRate *= multiplier;
            Destroy(other);
            Invoke("Reset", duration); // Calls method Reset after a period of time
        }
    }

    void Reset()
    {
        RaycastShooting.fireRate = 4;
    }

}

答案 1 :(得分:1)

您需要启动协程,而不是调用代答功能。

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

public class Powerup : MonoBehaviour {

    public float multiplier = 3f;
    public float duration = 10f;


    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            StartCoroutine(Pickup(other));
        }
    }

    IEnumerator Pickup(Collider player)
    {
        RaycastShooting.fireRate *= multiplier;
        Destroy(player.gameObject);              
        yield return new WaitForSeconds(duration);
        RaycastShooting.fireRate = 4;    
    }
}

请注意我已将Destroy(gameObject);更改为Destroy(player.gameObject);,因为我认为这就是您的意思。

我希望这能解决你的问题!