Exemple #1
0
    // called once every second
    void Update()
    {
        // rotate radar at all times
        turretRadar.transform.Rotate(Vector3.forward * turretRadarRotateSpeed, Space.Self);

        // rotate head at all times
        rotateTurretHead();

        // get target position
        target = targetObject.transform.position;

        // find the distance from turret to target
        targetDistance = Vector3.Distance(gameObject.transform.position, target);
        Debug.Log(targetObject.name + " Distance: " + targetDistance);

        // find if target is in range
        targetInRange = (targetDistance < turretRange) ? true : false;

        // tracking target rotate accordingly
        if (targetInRange)
        {
            // rotate base of turret
            target.y           = transform.position.y;
            targetRotation     = Quaternion.LookRotation(target - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * lookSpeed);

            // ToDo rotate turretHead along rotation.x to align with target

            // fire primary
            weaponsController.FirePrimaryWeapon();

            // rapid fire secondary
            for (int i = 0; i < 5; i++)
            {
                weaponsController.FireSecondaryWeapon();
            }
        }
        else // not tracking target... rotate casually
        {
            // auto rotate base of turret
            transform.Rotate(Vector3.up * turretRotateSpeed, Space.Self);
        }
    }
    void Update()
    {
        if (Time.timeScale == 0)
        {
            return;
        }

        if (Input.GetButton("Fire1"))
        {
            weaponsController.FirePrimaryWeapon();
        }
        if (Input.GetButton("Fire2"))
        {
            weaponsController.FireSecondaryWeapon();
        }
        if (Input.GetButton("Fire3"))
        {
            weaponsController.FireTertiaryWeapon();
        }
    }