Unity2D:如何让我的玩家在X时间内刀枪不入

本文关键字:时间 刀枪不入 玩家 我的 Unity2D | 更新日期: 2023-09-27 17:58:34

我想让我的玩家在重置回游戏中心后的几秒钟内免受物体的伤害,这意味着我不想让任何东西伤害他,也不想让玩家移动5秒钟,但我不知道该怎么做!我搜索了一下,但发现的结果与我的代码不匹配。不管怎样,这是我的剧本:

private Animator anim;
public float speed = 15f;
public static Vector3 target;
private bool touched;
private bool canmove;
Vector3 startPosition;

void Start () {
    target = transform.position;
    anim = GetComponent<Animator> ();
}
void Update () {
    if (Input.GetMouseButtonDown (0)) {
        Vector3 mousePosition = Input.mousePosition;
        mousePosition.z = 10; // distance from the camera
        target = Camera.main.ScreenToWorldPoint(mousePosition);
        target.z = transform.position.z;
    }
        transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
    var movementDirection = (target - transform.position).normalized;
    if (movementDirection.x != 0 || movementDirection.y != 0) {
        anim.SetBool ("walking", false);
        anim.SetFloat("SpeedX", movementDirection.x);
        anim.SetFloat("SpeedY", movementDirection.y);
        anim.SetBool ("walking", true);
    }
}
void FixedUpdate () {
    float LastInputX = transform.position.x - target.x;
    float LastInputY = transform.position.y - target.y;
    if (touched) {
        if (LastInputX != 0 || LastInputY != 0) {
            anim.SetBool ("walking", true);
            if (LastInputX < 0) {
                anim.SetFloat ("LastMoveX", 1f);
            } else if (LastInputX > 0) {
                anim.SetFloat ("LastMoveX", -1f);
            } else {
                anim.SetFloat ("LastMoveX", 0f);
            }
            if (LastInputY > 0) {
                anim.SetFloat ("LastMoveY", 1f);
            } else if (LastInputY < 0) {
                anim.SetFloat ("LastMoveY", -1f);
            } else {
                anim.SetFloat ("LastMoveY", 0f);
            }
        }
    }else{
        touched = false;
        anim.SetBool ("walking", false);            
    }
}

}

这是我的玩家健康脚本:

public int curHealth;
public int maxHealth = 3;
Vector3 startPosition;
bool  invincible = false;
void Start ()
{
    curHealth = maxHealth;
    startPosition = transform.position;
}

void Update ()
{
    if (curHealth > maxHealth) {
        curHealth = maxHealth;
    }
    if (curHealth <= 0) {
        Die ();
    }
}
void Die ()
{
    //Restart
    Application.LoadLevel (Application.loadedLevel);
}

public void Damage(int dmg)
{
    curHealth -= dmg;
    Reset();
}
void Reset ()
{
    transform.position = startPosition;
    PlayerMovement.target = startPosition;
}

}

所以更简单地说,我想让我的玩家在5秒内不会被敌人击中(一旦他回到屏幕中央),而玩我的玩家的人在5秒内不能移动。非常感谢。(我的游戏是一个自上而下的2d点击移动游戏)

Unity2D:如何让我的玩家在X时间内刀枪不入

我认为这应该能奏效。至少要给你指明正确的方向。

简单的解释,我变的不可战胜是一个时代,一个未来的时代。然后,我们只是在某个事件中添加一个未来的时间,从那个事件开始到未来的某个时刻,我们希望他不可战胜。

因此,在每一次他会受到伤害的事件中,我们都会检查,现在是不可战胜的未来,还是现在是不可击败的未来。那是你的屁股。

DateTime invincible = DateTime.Now;
public void Damage(int dmg)
{
    if(invincible <= DateTime.Now)
    {
        curHealth -= dmg;
        Reset();
    }
}
void Reset ()
{
    transform.position = startPosition;
    PlayerMovement.target = startPosition;
    invincible = DateTime.Now.AddSeconds(5);
}

如果你想要像这样的辅助方法

bool IsInvincible(){
    return invincible <= DateTime.Now;
}

然后,您可以在public void Damage(int dmg) 中将if(invincible <= DateTime.Now)更改为if(IsInvincible())

在游戏开发中,您必须记住,所有事情都发生在帧更新中。我喜欢使用协同程序在Unity中实现不可战胜的框架。协程只是一个与主更新循环并行运行的方法,并在下一次更新中从停止的地方恢复。

float dmg = 100.0f;
bool isHitByEnemy = false
bool isInvulnerable = false
void Demage()
{
    //some dmg logic
    if (isHitByEnemy && isInvulnerable == false)
    {
        StartCoroutine("OnInvulnerable");
    }
}
IEnumerator OnInvulnerable()
{
    isInvulnerable  = true;
    dmg = 0.0f; 
    yield return new WaitForSeconds(0.5f); //how long player invulnerable
    dmg = 100.0f;
    isInvulnerable  = false;
}