碰撞后如何在一定时间内持续通电?

时间:2019-03-22 03:34:46

标签: c# unity3d

我正在制作一个怪异的小跑步游戏,并且尝试添加一种加电功能,使玩家可以跳跃一小段时间,也许是10秒。

using UnityEngine;

public class PlayerCollision : MonoBehaviour {

public PlayerMovement movement;
public GameObject PowerUp;
public float jumpUpForce = 5000f;
public float jumpBackForce = 1000f;
public Rigidbody rb;


void OnCollisionEnter(Collision collisionInfo)
{
    if (collisionInfo.collider.tag == "Obstacle")
    {
        movement.enabled = false;
    }

    if (collisionInfo.collider.tag == "PowerUp")
    {
        PowerUp.SetActive(false);
        JumpPower();
    }
}

void JumpPower()
{
    if (transform.position.y < 2)
    {
        if (Input.GetKey("left shift"))
        {
            rb.AddForce(0, jumpUpForce * Time.deltaTime, -jumpBackForce * Time.deltaTime);
        }
    }
}

我知道问题在于,当玩家与加电碰撞并运行JumpPower()函数时,它仅调用一次,因此无法检测到按下的左移行为(我认为)。 / p>

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您可以使用协程。每次上电时,您都将取消已经存在的协程(如果存在)并开始新的协程,这将处理时间。这就是我将要完成的方式:

public PlayerMovement movement;
public GameObject PowerUp;
public float jumpUpForce = 5000f;
public float jumpBackForce = 1000f;
public Rigidbody rb;

bool doWeHaveJumpPowerUp;
Coroutine cor;
public float timer;

// Use this for initialization
void Start()
{
    doWeHaveJumpPowerUp = false;
    timer = 10.0f;
}


void OnCollisionEnter(Collision collisionInfo)
{
    if (collisionInfo.collider.tag == "Obstacle")
        movement.enabled = false;

    if (collisionInfo.collider.tag == "PowerUp")
    {
        PowerUp.SetActive(false);
        if(cor != null)
        {
            StopCoroutine(cor);
            cor = null;
        }
        cor = StartCoroutine(JumpPower());
    }
}

IEnumerator JumpPower()
{
    doWeHaveJumpPowerUp = true;
    yield return new WaitForSeconds(timer);
    doWeHaveJumpPowerUp = false;
}

// Update is called once per frame
void Update()
{
    if (doWeHaveJumpPowerUp)
        if (transform.position.y < 2)
            if (Input.GetKey("left shift"))
                rb.AddForce(0, jumpUpForce * Time.deltaTime, -jumpBackForce * Time.deltaTime);
}

编辑:如果您想要更多类型的电源,这就是您要添加的方式:

public PlayerMovement movement;
public GameObject PowerUp;
public float jumpUpForce = 5000f;
public float jumpBackForce = 1000f;
public Rigidbody rb;

bool doWeHaveJumpPowerUp;
Coroutine cor;
public float timer;

// 2nd power up
bool doWeHaveFlyPowerUp;

// Use this for initialization
void Start()
{
    doWeHaveJumpPowerUp = false;
    timer = 10.0f;
}


void OnCollisionEnter(Collision collisionInfo)
{
    if (collisionInfo.collider.tag == "Obstacle")
        movement.enabled = false;

    if (collisionInfo.collider.tag == "PowerUpJump")
    {
        PowerUp.SetActive(false);
        if (cor != null)
        {
            StopCoroutine(cor);
            cor = null;
        }
        cor = StartCoroutine(JumpPower());
    }

    if (collisionInfo.collider.tag == "PowerUpFly")
    {
        PowerUp.SetActive(false);
        if (cor != null)
        {
            StopCoroutine(cor);
            cor = null;
        }
        cor = StartCoroutine(FlyPower());
    }
}

IEnumerator JumpPower()
{
    doWeHaveJumpPowerUp = true;
    yield return new WaitForSeconds(timer);
    doWeHaveJumpPowerUp = false;
}

IEnumerator FlyPower()
{
    doWeHaveFlyPowerUp = true;
    yield return new WaitForSeconds(timer);
    doWeHaveFlyPowerUp = false;
}


// Update is called once per frame
void Update()
{
    if (doWeHaveJumpPowerUp)
        if (transform.position.y < 2)
            if (Input.GetKey("left shift"))
                rb.AddForce(0, jumpUpForce * Time.deltaTime, -jumpBackForce * Time.deltaTime);
    if (doWeHaveFlyPowerUp)
    {
        //Fly power up logic
    }
}

在这两个示例中,加电都会取消现有的加电。您应该计划上电的工作方式:

  • 角色应该能够同时具有很多能量提升功能。
  • 应该重新获得与您已经具有的持续时间相同类型的通电,或者延长其持续时间。

此外,此刻您将只能进行一次通电。如果您想要更多,则必须为它们编写一个生成器(如果您希望它们是随机的),或者如果您希望它们按某种顺序放置,请手动放置它们。

答案 1 :(得分:0)

我不熟悉Unity编码,但是对于您要完成的工作,听起来好像您需要运行一个Timer。

我通常会将以下代码放在Form.Load()中,但是,由于这不是实际的winforms应用程序,因此您可能会更清楚地知道在运行时将其放置在何处。

int t = 0; // Add this as a variable within the class to provide sufficient scope
Timer power_up_timer = new Timer();
power_up_timer.Interval = 1000; // 1000 ms = 1 second
power_up_timer.Tick += power_up_timer_Tick;

然后在班级内的任何地方添加“滴答”事件:

private void power_up_timer_Tick(object sender, EventArgs e)
{
    if (t == 10) { /* End the power-up */ t = 0; power_up_timer.Stop(); }
    else { t++; }
}

然后,在加电初始化块中添加:

power_up_timer.Start();

无论如何,您都可以在代码中进行处理,这几乎就是完成的过程。

答案 2 :(得分:0)

您还可以运行协程!

// action is just a function or a method
// if you need that with arguments it would be System.Action< T > action
    IEnumerator TimerRoutine(int seconds, System.Action action)
    {
        int sec = 0;
        float delta = 0;
        while (true)
        {
            delta += Time.deltaTime;
            Debug.Log(delta);
            if(delta >= 1f)
            {
                sec += 1;
                delta = 0;
            }
            if (sec == seconds)
                break;
            else
                yield return null;
        }

        // this will start your function at the end of the timer
        action();
    }

您可以调用StartCoroutine(TimerRoutine(args))从任何MonoBehaviour中启动计时器。

相关问题