Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var turretHead : Transform;
- var turretBarrel : Transform;
- var target : Transform;
- var rotationToLook : Quaternion;
- var rotationEuler : Vector3;
- var maxVerticalLook : float = 45;
- var maxHorizontalLook : float = 45;
- var maxDistance : float = 30;
- var distance : float;
- var targetLocked : boolean = false;
- var targetAquiring : boolean = false;
- var targetFiring : boolean = false;
- var rocket : Rigidbody; //our prefab
- var throwPower : float = 500; //power variable determines how fast this object will be "shot out"
- var lookAt : Transform;
- function Update () {
- // Calculate relative position to calculate rotation and distance.
- var relativePos = target.position - transform.position;
- // Calculate Distance of Target
- distance = Vector3.Distance(transform.position,target.position);
- // Calculate Rotation
- rotationToLook = Quaternion.LookRotation(relativePos);
- rotationEuler = rotationToLook.eulerAngles; // Used for debugging
- // Check if target is in range (not yet done) and also if in targeting area
- if ( distance < maxDistance && checkYBounds( rotationToLook, maxHorizontalLook ) && checkXBounds( rotationToLook, maxVerticalLook ) ) {
- // Debug.Log("Turret is in Range");
- if ( targetAquiring == false ) {
- // Starting to Aquire
- StartCoroutine("acquiringTarget");
- } if ( targetLocked == true && targetFiring == false ) {
- // Fire Bullet
- StartCoroutine("fireBullet");
- }
- } else {
- // Revert to Home Position
- rotationEuler = Vector3 ( 0, 0, 0 );
- }
- // Move Turret Head
- var turretHeadEuler : Vector3;
- turretHeadEuler.y = rotationEuler.y;
- var turretHeadqr : Quaternion = Quaternion.Euler(turretHeadEuler);
- turretHead.localRotation = Quaternion.Lerp(turretHead.localRotation, turretHeadqr, Time.deltaTime * 5);
- var turretBarrelEuler : Vector3;
- turretBarrelEuler.z = -rotationEuler.x;
- turretBarrelEuler.y = 0;
- var turretBarrelqr : Quaternion = Quaternion.Euler(turretBarrelEuler);
- turretBarrel.localRotation = Quaternion.Lerp(turretBarrel.localRotation, turretBarrelqr, Time.deltaTime * 5);
- }
- function checkYBounds( rotationToLook : Quaternion, maxLook : float ) {
- if ( rotationToLook.eulerAngles.y < maxLook || rotationToLook.eulerAngles.y > ( 360 - maxLook ) ) {
- return true;
- } else {
- return false;
- }
- }
- function checkXBounds( rotationToLook : Quaternion, maxLook : float ) {
- if ( rotationToLook.eulerAngles.x < maxLook || rotationToLook.eulerAngles.x > ( 360 - maxLook ) ) {
- return true;
- } else {
- return false;
- }
- }
- function acquiringTarget() {
- targetAquiring = true;
- yield WaitForSeconds(3);
- targetLocked = true;
- Debug.Log("Auto Turret - Target Acquired - Ready to fire");
- }
- function fireBullet() {
- targetFiring = true;
- var clone : Rigidbody; //variable within a variable
- clone = Instantiate(rocket, rocket.transform.position, rocket.transform.rotation); //the clone variable holds our instantiate action
- clone.gameObject.SetActive( true );
- clone.rigidbody.AddForce(clone.transform.forward * throwPower);
- yield WaitForSeconds(1);
- targetFiring = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement