【问题标题】:Unity 5 animation triggers (Javascript)Unity 5 动画触发器 (Javascript)
【发布时间】: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 文档中的 GetComponentAnimation!在您的代码中,var GetComponent.&lt;animation&gt;("Dead"); 在多种方面都是错误的。与您的最后一个函数相比,if(health &gt;= 0) GetComponent.&lt;animation&gt;("Dead"); { ... } 行没有意义
  • 感谢您的链接。你会建议我做什么来修复代码?
  • 看我的回答。这真的很难,因为我现在不太了解您的项目结构以及您如何准确地连接事物。也许在 Unity 中查看 Inspector 的屏幕截图会有所帮助,更好地描述究竟会发生什么以及如何发生

标签: javascript animation unity3d


【解决方案1】:

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

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

而且我不得不承认我通常使用 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
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    相关资源
    最近更新 更多