在脚本预制

时间:2015-08-13 23:26:10

标签: c# unity3d

我试图制造一些可以被玩家和玩家使用的咒语。敌人。 所以,我已经创建了一个脚本" BasicAttack"如下所示。

public class BasicAttak : Spell {
    public int          dmg = 2;
    public float        speed = 10.0f;
    public Rigidbody    projectile; 
    private float       currentCooldown = 0.0f;

    void Awake()
    {
        currentCooldown = 0.0f;
    }

    public override bool    launch(Vector3 launcherPosition, Vector3 targetPosition)
    {
        Debug.Log("Launch Basic Attack, cooldown = " + this.currentCooldown + " - if = " + (this.currentCooldown <= 0.0f) );
        if (this.currentCooldown <= 0.0f) {
            if (projectile == null)
                return (false);
            Quaternion r = Quaternion.LookRotation (targetPosition - launcherPosition);
            Rigidbody shot = Instantiate (projectile, launcherPosition, r) as Rigidbody;
            ProjectileScript a = shot.GetComponent<ProjectileScript>();
            a.damage = this.dmg;
            a.speed = this.speed;
            this.currentCooldown = this.cooldown;
            return (true);
        }
        return (false);
    }

    void    Update()
    {
        if (this.currentCooldown > 0.0f)
            this.currentCooldown -= Time.deltaTime;
        Debug.Log("currentCooldown = "+currentCooldown);
    }
}

因为我想使用这个脚本创建不同类型的法术(使用伤害和速度参数),我创建了一个GameObject,每个法术一个。

我已将脚本分配给每个Gameobject,并为每个变量设置不同的值。 然后,一旦完成所有配置,我就为这样的法术创建了预制件: Unity Spell Prefab

到目前为止一切似乎都很好。所以,我已经将我的预制件分配给了玩家拼插槽,我已经启动了游戏,并且我试图发射一个咒语。 第一个咒语正在工作,但是对于第二个咒语,变量currentCooldown永远不会被重置。 这是我在控制台中得到的内容。

  

启动基本攻击,冷却= 0.5 - if = False

     

UnityEngine.Debug:Internal_Log(Int32,String,Object)   UnityEngine.Debug:Log(Object)BasicAttak:launch(Vector3,Vector3)(at   资产\ MyAsset \脚本\法术\ BasicAttak.cs:18)   ACharacter:LaunchSpell(Int32)(at   Assets \ MyAsset \ Scripts \ ACharacter.cs:70)Player:FixedUpdate()(at   Assets \ MyAsset \ Scripts \ Player.cs:20)(文件名:   Assets / MyAsset / Scripts / Spell / BasicAttak.cs Line:18)

     

currentCooldown = 0

     

UnityEngine.Debug:Internal_Log(Int32,String,Object)   UnityEngine.Debug:Log(Object)BasicAttak:Update()(at   资产\ MyAsset \脚本\拼写\ BasicAttak.cs:42)

在完全相同的帧中,currentCooldown变量在启动函数中显示为0.5,在更新函数中显示为0。如果它在一个预制件中的完全相同的脚本中是相同的变量。

如果我想发射另一个咒语,我就不能再停下来玩游戏了。我必须重启Unity才能启动我的第一个咒语。

这对我来说似乎很神秘,我甚至不确定要问哪个问题。如何为变量设置相同的值?

注意:如果我不是将预制件分配给我的玩家拼写槽,而是从场景中分配一个GameObject,这个问题就不会发生了。但是,我无法真正使用这个&#34;解决方案&#34;因为我必须为每个也会使用该法术的敌人创建一个GameObject。这意味着如果我有786个敌人,我需要创建786 GameObject并手动为每个敌人分配该法术。

1 个答案:

答案 0 :(得分:2)

这种情况正在发生,因为在Unity中,大多数情况Update方法比FixedUpdate方法更频繁地调用。事实上Update每帧调用一次,但FixedUpdate方法的频率取决于您的Fixed Timestep设置(查看Edit > Project Settings > Time)。它可以在每3帧后调用一次。因此问题修复更新显示某些内容,更新显示其他内容。

作为修复,您应该使用Invoke而不是更新。这是

的方式
public class BasicAttak : Spell {
    public int          dmg = 2;
    public float        speed = 10.0f;
    public Rigidbody    projectile; 
    private float       currentCooldown = 0.0f;

    void Awake()
    {
        currentCooldown = 0.0f;
    }

    public override bool    launch(Vector3 launcherPosition, Vector3 targetPosition)
    {
        Debug.Log("Launch Basic Attack, cooldown = " + this.currentCooldown + " - if = " + (this.currentCooldown <= 0.0f) );
        if (this.currentCooldown <= 0.0f) {
            if (projectile == null)
                return (false);
            Quaternion r = Quaternion.LookRotation (targetPosition - launcherPosition);
            Rigidbody shot = Instantiate (projectile, launcherPosition, r) as Rigidbody;
            ProjectileScript a = shot.GetComponent<ProjectileScript>();
            a.damage = this.dmg;
            a.speed = this.speed;
            this.currentCooldown = this.cooldown;
            Invoke("ResetCoolDown", this.cooldown);
            return (true);
        }
        return (false);
    }

    void ResetCoolDown()
    {
        this.currentCooldown = 0.0f;
    }
}

如果您不了解Invoke

public void Invoke(string methodName, float time);

会在string methodName参数中提供时间之后调用方法(float time参数中提供的方法名称),因此Invoke("ResetCoolDown", this.cooldown); Invoke会调用方法ResetCoolDown在您的情况下this.cooldown时间(设置为0.5f)之后。