所以我有一个玩家会在与敌人发生碰撞时开始制作“受伤”动画。 2秒后,他将重新回到他的起始位置,但“伤害”动画不会停止。即使玩家四处走动,它也会继续播放。
这是我的整个播放器脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
public float speed = 3.0f;
public Vector2 jumpHeight;
[HideInInspector] public bool facingRight = true;
private float minX = -10.5f;
private float maxX = 10.5f;
public int count;
public Text countText;
private Animator anim;
public GameObject NECText;
public GameObject WinText;
public GameObject player;
void Start()
{
anim = GetComponent<Animator>();
transform.position = new Vector3(-10.0f, -2.32f, 0);
count = 0;
countText.text = "Coins: " + count.ToString();
NECText.gameObject.SetActive(false);
WinText.gameObject.SetActive(false);
}
void Update()
{
Movement();
Death();
float horizontalInput = Input.GetAxis("Horizontal");
if(horizontalInput > 0 && !facingRight)
{
Flip();
}
else if(horizontalInput < 0 && facingRight)
{
Flip();
}
}
void Movement()
{
float horizontalInput = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * horizontalInput * speed * Time.deltaTime);
if(Input.GetKeyDown(KeyCode.Space)) //fix unlimited jump
{
GetComponent<Rigidbody2D>().AddForce(jumpHeight, ForceMode2D.Impulse);
}
if(transform.position.x < minX)
{
transform.position = new Vector3 (minX, transform.position.y, 0);
}
else if(transform.position.x > maxX)
{
transform.position = new Vector3(maxX, transform.position.y, 0);
}
}
void Death()
{
if (transform.position.y <= -4.25f)
{
anim.SetBool("isHurt", true);
Invoke("Respawn", 2f);
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enemy")
{
anim.SetBool("isHurt", true);
Invoke("Respawn", 2f);
}
else if (collision.gameObject.tag == "Coin")
{
collision.gameObject.SetActive(false);
count++;
countText.text = "Coins: " + count.ToString();
}
else if(collision.gameObject.tag == "Chest" && count < 3)
{
NECText.gameObject.SetActive(true);
Destroy(NECText, 5.0f);
}
else if(collision.gameObject.tag == "Chest" && count == 3)
{
WinText.gameObject.SetActive(true);
Destroy(WinText, 5.0f);
anim.SetBool("isWinning", true);
}
}
void Respawn()
{
Destroy(player);
Instantiate(player, new Vector3(-10.0f, -2.32f, 0), Quaternion.identity);
anim.SetBool("isHurt", false);
}
}
我知道它可能非常凌乱,但这是我的第一款没有教程的游戏,所以很难。
答案 0 :(得分:3)
根据上述评论中的讨论,OP遇到与 Repsawn()功能相关的问题。
您可以采用的另一种方法是更改 Respawn()功能。 而不是实例化播放器预制件,您可以重复使用它 预制并继续调整其值。在这种情况下,你 不需要创建单独的类来存储上面提到的 值。你只是这样说
tags.forEach(function(value) {
var where1 = {};
var attr1 = ['id'];
attr1 = ['id'];
where1['name'] = value;
tagData['name'] = value;
saveTag(tagModel, quick_start_tagModel, where1, attr1, result1, tagData);
});
//Create a function so that tagData will have a different scope for each call
function saveTag(tagModel, quickStartTagModel, where, attr, result1, tagData) {
tagModel.getTag(where, attr, function(code,result){
if(result.length!=0) {
var quickStartTagData = {'quick_start_id' : result1['id'], 'tag_id' :result[0]['id']}
quickStartTagModel.saveData(quickStartTagData, function(code, result){});
console.log(quickStartTagData);
} else {
tagModel.saveData(tagData, function(code,result) {});
}
});
}
这应该解决 如果你重写你的重生代码你的问题
通过更改重新生成的代码以仅重用单个游戏对象,问题似乎得到解决。