【发布时间】:2018-04-30 19:13:14
【问题描述】:
我在 unity 5 中工作,我正在尝试让动画正常工作。当按下按钮“Fire1”又名左键时,我的敌人的生命值会下降,直到达到零生命值。
当生命值达到零时,应该播放死亡动画。
GetComponent<animation>("Dead") 或 play.animation("Dead") 似乎都不起作用。
我也试过GetComponent.<animation>("Dead")。
由于< 和>,代码的第一个sn-p 似乎不起作用。第二个 sn-p 不起作用,因为 . 和 play (即使我为它做了一个变量)。感谢您提供任何帮助。我的代码是
#pragma strict
var range: float = 1.8;
var attackInterval: float = 0.7;
var meleeDamage: float = 30;
var GetComponent.<animation>("Dead");
private var nextAttack: float = 0;
function MeleeAttack()
{
if (Time.time > nextAttack)
{ // only repeat attack after attackInterval
nextAttack = Time.time + attackInterval;
// get all colliders whose bounds touch the sphere
var colls: Collider[] = Physics.OverlapSphere(transform.position,
range);
for (var hit : Collider in colls)
{
if (hit && hit.tag == "Enemy")
{ // if the object is an enemy...
// check the actual distance to the melee center
var dist = Vector3.Magnitude(hit.transform.position -
transform.position);
if (dist <= range)
{ // if inside the range...
// apply damage to the hit object
hit.SendMessage("ApplyDamage", meleeDamage);
}
}
}
}
}
function Update()
{
if (Input.GetButtonDown("Fire1"))
{
MeleeAttack();
}
}
var health: float = 100;
function ApplyDamage(damage: float)
{
if (health > 0)
{ // if enemy still alive (don't kick a dead dog!)
health -= damage; // apply the damage...
// <- enemy can emit some sound here with audio.Play();
if (health <= 0)
GetComponent.<animation>("Dead");
{ // if health has gone...
// enemy dead: destroy it, explode it etc.
}
}
}
【问题讨论】:
-
请参阅 Unity 文档中的 GetComponent 和 Animation!在您的代码中,
var GetComponent.<animation>("Dead");在多种方面都是错误的。与您的最后一个函数相比,if(health >= 0) GetComponent.<animation>("Dead"); { ... }行没有意义 -
感谢您的链接。你会建议我做什么来修复代码?
-
看我的回答。这真的很难,因为我现在不太了解您的项目结构以及您如何准确地连接事物。也许在 Unity 中查看 Inspector 的屏幕截图会有所帮助,更好地描述究竟会发生什么以及如何发生
标签: javascript animation unity3d