Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GenericTurret : MonoBehaviour {
  5.  
  6. //this code is designed to be mounted on a "turret holder." The gameobject this script lives on doesn't turn when targeting.
  7.  
  8. //child objects
  9. public Transform turret;
  10. public Transform gun;
  11.  
  12. //the direction we want the guns to face.
  13. public Vector3 targetDirection = Vector3.right;
  14.  
  15. //tweakables
  16. public float maxYaw = 80f;
  17. public float minYaw = -80f;
  18.  
  19. public float maxPitch = 10f;
  20. public float minPitch = -45f;
  21.  
  22. public float yawSpeed = 30f; //in degrees per second.
  23. public float pitchSpeed = 60f; //in degrees per second.
  24.  
  25. // Update is called once per frame
  26. void FixedUpdate()
  27. {
  28.  
  29.  
  30. //convert targeting to local reference frame
  31. Vector3 localTarget = transform.InverseTransformDirection(targetDirection).normalized;
  32.  
  33. //get the component angles
  34. float _yaw = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
  35. float _pitch = Mathf.Atan2(-localTarget.y, localTarget.z) * Mathf.Rad2Deg;
  36.  
  37. //clam our rotations
  38. _yaw = Mathf.Clamp(_yaw, minYaw, maxYaw);
  39. _pitch = Mathf.Clamp(_pitch, minPitch, maxPitch);
  40.  
  41. Debug.Log(_yaw);
  42.  
  43. //turn them into quaternions.
  44. Quaternion desiredYaw = Quaternion.AngleAxis(_yaw, Vector2.up);
  45. Quaternion desiredPitch = Quaternion.AngleAxis(_pitch, Vector2.right);
  46.  
  47. turret.localRotation = Quaternion.RotateTowards(turret.localRotation, desiredYaw, yawSpeed * Time.fixedDeltaTime);
  48. // gun.localRotation = Quaternion.RotateTowards(gun.localRotation, desiredPitch, pitchSpeed * Time.fixedDeltaTime);
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement