Esempio n. 1
0
    void Start()
    {
        if (!playerScript)
        {
            if (GetComponent <ST_Player>())
            {
                playerScript = GetComponent <ST_Player>();
            }
        }
        for (int t = 0; t < turrets.Length; t++)
        {
            if (turrets[t].turretScript)
            {
                turrets[t].platformScript = turrets[t].turretScript.GetComponent <MF_AbstractPlatform>();
            }
        }

        SelectTurret(selectedTurret);
    }
	void Update () {
		if (error == true) { return; }

		// player or AI control
		switch (controller) {
		case ControlType.Player :  // player will move aim object and hold mouse to fire
			if ( player ) { // make sure player is defined
				// detect change in player
				if ( player != playerToggle ) {
					playerToggle = player;
					playerScript = player.GetComponent<ST_Player>();
					target = playerScript.aimObject;
				}

				if ( playerScript.turretControl == true ) {
					target = playerScript.aimObject;
					if ( weapons.Length > 0 ) {
						if ( Input.GetMouseButton(0) ) {
							Shoot();
						}
					}
				}
			} else { // no player defined
				target = null;
			}
			break;

		case ControlType.AI_AutoTarget : // AI will aim, getting targets from targeting script
			if ( targetingScript ) { // make sure targeting script exsists
				target = targetingScript.weaponTarget;
			} else {
				target = null;
			}
			break;

		case ControlType.AI_NoTarget : // AI will aim, but targets must be supplied some other way
			break;

		default :
			break;
		}

		// if this is a new target, cache info
		if (target != storedTarget) {
			storedTarget = target;
			if (target) { // in case target became null
				if (checkTargetSize == true) {
					Bounds _bounds; 
					targetSize = 0f;
					if (target.GetComponent<Collider>()) { // does root have a collider?
						_bounds = target.GetComponent<Collider>().bounds;
						targetSize = Mathf.Max( _bounds.size.x * target.transform.localScale.x, _bounds.size.y * target.transform.localScale.y, _bounds.size.z * transform.localScale.z );
					} else { // no colldier found on target root
						// check root object's children for the first collider found
						for (int c=0; c < target.transform.childCount; c++) {
							if (target.transform.GetChild(c).GetComponent<Collider>()) { // found a collider
								_bounds = target.transform.GetChild(c).GetComponent<Collider>().bounds;
								targetSize = Mathf.Max( _bounds.size.x * target.transform.localScale.x, _bounds.size.y * target.transform.localScale.y, _bounds.size.z * transform.localScale.z );
								break; // don't need to keep checking
							}
						}
						// _bounds still 0, so no colliders found, use default
						if (targetSize == 0) { targetSize = targetSizeDefault; }
					}
				} else {
					// not checking size, use default
					targetSize = targetSizeDefault;
				}
			}
		}

		turretScript.target = target; // pass target to turret

		if (target) {
			if ( controller == ControlType.AI_AutoTarget || controller == ControlType.AI_NoTarget ) { // AI will shoot
				if ( weapons.Length > 0 ) {
					// set shot speed for turret aim intercept
					turretScript.shotSpeed = 1000f;
					if ( weapons[curWeapon].script.ReadyCheck() == true ) { // early out if weapon isn't ready
							Shoot();
						}
					
				}
			}
		}
		
		// set angle of weapons to converge based on current target range
		if (dynamicConverge == true && weapons.Length > 0 ) {
			float _convergeRange = Mathf.Clamp(    Vector3.Distance(turretScript.weaponMount.transform.position, turretScript.targetLocation),    minConvergeRange, Mathf.Infinity );
			for (int w = 0; w < weapons.Length; w++) {
				if (weapons[w] != null) {
					Quaternion _rotGoal;
					if (target) { // slew to target
						_rotGoal = Quaternion.LookRotation( turretScript.weaponMount.transform.position +
						                                   (turretScript.weaponMount.transform.forward * _convergeRange) -
						                                   weapons[w].weapon.transform.position, turretScript.weaponMount.transform.up );
					} else { // reset converge to none 
						_rotGoal = Quaternion.LookRotation( weapons[w].weapon.transform.position +
						                                   (turretScript.weaponMount.transform.forward * 1000f) -
						                                   weapons[w].weapon.transform.position, turretScript.weaponMount.transform.up );
					}
					weapons[w].weapon.transform.rotation = Quaternion.RotateTowards( weapons[w].weapon.transform.rotation, _rotGoal, convergeSlewRate * Time.deltaTime );
				}
			}
		}
		
	}