因此,每当骷髅碰到玩家/角色时,它不会显示单词“ HIT!”。我做错了什么?
骨架敌人脚本
private Rigidbody2D myBody;
[Header("Movement")]
public float moveSpeed;
private float minX, maxX;
public float distance;
public int direction;
private bool patrol, detect;
private Transform playerPos;
private Animator anim;
[Header("Attack")]
public Transform attackPos;
public float attackRange;
public LayerMask playerLayer;
public int damage;
//sound
无效觉
void Awake()
{
anim = GetComponent<Animator>();
playerPos = GameObject.Find("George").transform;
myBody = GetComponent<Rigidbody2D>();
}
无效启动
private void Start()
{
maxX = transform.position.x + (distance);
minX = maxX - distance;
//if (Random.value > 0.5) direction = 1;
//else direction = -1;
}
无效更新
void Update()
{
if (Vector3.Distance(transform.position, playerPos.position) <= 4.0f) patrol = false;
else patrol = true;
}
无效的固定更新
private void FixedUpdate()
{
if (anim.GetBool("Death"))
{
myBody.velocity = Vector2.zero;
GetComponent<Collider2D>().enabled = false;
myBody.isKinematic = true;
anim.SetBool("Attack", false);
return;
}
if (myBody.velocity.x > 0)
{
transform.localScale = new Vector2(1f, transform.localScale.y);
anim.SetBool("Attack", false);
}
else if
(myBody.velocity.x < 0) transform.localScale = new Vector2(-1f, transform.localScale.y);
if (patrol)
{
detect = false;
switch (direction)
{
case -1:
if (transform.position.x > minX)
myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
else
direction = 1;
break;
case 1:
if (transform.position.x < maxX)
myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
else
direction = -1;
break;
}
}
else
{
if (Vector2.Distance(playerPos.position, transform.position) >= 1.0f)
{
if (!detect)
{
detect = true;
anim.SetTrigger("Detect");
myBody.velocity = new Vector2(0, myBody.velocity.y);
}
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Detect")) return;
Vector3 playerDir = (playerPos.position - transform.position).normalized;
if (playerDir.x > 0)
myBody.velocity = new Vector2(moveSpeed + 0.4f, myBody.velocity.y);
else
myBody.velocity = new Vector2(-(moveSpeed + 0.4f), myBody.velocity.y);
}
else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
{
myBody.velocity = new Vector2(0, myBody.velocity.y);
anim.SetBool("Attack", true);
}
}
}
无效攻击
public void Attack()
{
myBody.velocity = new Vector2(0, myBody.velocity.y);
Collider2D attackPlayer = Physics2D.OverlapCircle(attackPos.position, attackRange, playerLayer);
if (attackPlayer == null)
{
if(attackPlayer.tag == "Player")
{
print("Hit!");
attackPlayer.gameObject.GetComponent<PlayerHealth>().TakeDamage(damage);
}
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPos.position, attackRange);
}
玩家健康脚本
无效觉
public int health = 100;
void Awake()
{
}
无效更新
void Update()
{
if (health < 1)
{
print("Dead");
}
}
禁止破坏
public void TakeDamage(int damage)
{
FindObjectOfType<CameraShake>().ShakeItMedium();
health -= damage;
}
private void OnTriggerEnter2D(Collider2D target)
{
if(target.tag == "Fireball")
{
TakeDamage(25);
}
}
答案 0 :(得分:0)
尽管那堆长代码可能还有很多其他问题:
if (attackPlayer == null)
{
if(attackPlayer.tag == "Player")
{
print("Hit!");
attackPlayer.gameObject.GetComponent<PlayerHealth>().TakeDamage(damage);
}
}
您检查attackPlayer是否为空,然后尝试对其进行处理...我很确定您是说if (attackPlayer != null)
...
答案 1 :(得分:0)
您似乎没有在脚本中调用“攻击”方法。
else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
{
myBody.velocity = new Vector2(0, myBody.velocity.y);
// calling the attack method, so the physics cast is being made
Attack();
anim.SetBool("Attack", true);
}
别忘了更改此设置,您正在检查演员是否击中任何东西而不是击中东西。
// if the cast hit something
if (attackPlayer != null)
{
if(attackPlayer.tag == "Player")
{
print("Hit!");
attackPlayer.gameObject.GetComponent<PlayerHealth>().TakeDamage(damage);
}
}