コード例 #1
0
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /// <summary>
    //  Called each frame.
    /// </summary>
    protected override void Update()
    {
        base.Update();

        // Rotating turret weapon
        if (Head != null && Barrel != null)
        {
            // A valid target is known
            if (_AttackTarget != null && _ObjectState == WorldObjectStates.Active)
            {
                if (_AttackTarget.IsAlive())
                {
                    // Aim the turret's head at the target (Z AXIS ONLY)
                    Vector3 hDirection = (_AttackTarget.transform.position - transform.position).normalized;
                    hDirection.y = 0;
                    Quaternion hLookAtRot = Quaternion.LookRotation(hDirection);
                    Head.transform.rotation = Quaternion.Lerp(Head.transform.rotation, hLookAtRot, WeaponAimingSpeed * Time.deltaTime);

                    // Aim the turret's barrel at the target (Y AXIS ONLY)
                    Vector3 bDirection = (_AttackTarget.transform.position - transform.position).normalized;
                    bDirection.z = 0;
                    Quaternion bLookAtRot = Quaternion.LookRotation(Barrel.transform.forward, bDirection);
                    Barrel.transform.rotation = Quaternion.Lerp(Barrel.transform.rotation, bLookAtRot, WeaponAimingSpeed * Time.deltaTime);

                    if (TowerWeapon != null)
                    {
                        // Fire the turret's weapon
                        if (TowerWeapon.CanFire())
                        {
                            TowerWeapon.FireWeapon();
                        }
                    }
                }

                // Target is dead so null it out (so that the turret returns to the default state)
                else
                {
                    _AttackTarget = null;
                }
            }

            // Return to the default rotation
            else
            {
                Head.transform.rotation = Quaternion.Lerp(Head.transform.rotation, _DefaultRotation, WeaponAimingSpeed * Time.deltaTime);
            }
        }
    }