Unity 5 动画触发器 (Javascript)



我正在使用Unity 5,并且正在尝试使动画正常工作。当按下"Fire1"(又名左键点击(按钮时,敌人的生命值会下降,直到生命值为零。

当生命值达到零时,死亡动画应该播放。

GetComponent<animation>("Dead")play.animation("Dead")似乎都没有工作。

我也试过GetComponent.<animation>("Dead").

由于<>,第一段代码似乎不起作用。第二个片段不起作用,因为.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.
}
}
}

我只能猜测,因为我真的不明白你是如何尝试获得动画的。

我假设您希望能够在编辑器/检查器中拖入动画。

我不得不承认我通常使用 C#,不能保证这里的代码对于 Unityscript 是正确的。我标记了我进行更改的行(CHANGE(。我没有碰你的其余代码,所以不保证它在复制粘贴后有效。

最好看看我提供的链接

#pragma strict
var range: float = 1.8;
var attackInterval: float = 0.7;
var meleeDamage: float = 30;
/* 
* CHANGE 1
*
* https://unity3d.com/de/learn/tutorials/s/animation
* https://docs.unity3d.com/ScriptReference/Animation.html
*/
var deadAnimation : Animation; 
private var nextAttack: float = 0;
/* 
* CHANGE 2
* https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html
* https://unity3d.com/de/learn/tutorials/topics/scripting/getcomponent
*
* if you drag it in via the Inspector you don't need this
* but if you want to make it private and not drag it in via inspector 
* than you need to get the component here
*/
function Start(){
if(!deadAnimation){
deadAnimation = GetComponent<Animation>();
}
}
function MeleeAttack()
{
...
}
function Update()
{
...
}
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)
{ // if health has gone...
// enemy dead: destroy it, explode it etc.
/*
* CHANGE 3
* https://docs.unity3d.com/ScriptReference/Animation.Play.html
*
* I assume at some point you want to play the animtion here
*/
deadAnimation.Play
}
}
}

最新更新