Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class GenericTurret : MonoBehaviour {
- //this code is designed to be mounted on a "turret holder." The gameobject this script lives on doesn't turn when targeting.
- //child objects
- public Transform turret;
- public Transform gun;
- //the direction we want the guns to face.
- public Vector3 targetDirection = Vector3.right;
- //tweakables
- public float maxYaw = 80f;
- public float minYaw = -80f;
- public float maxPitch = 10f;
- public float minPitch = -45f;
- public float yawSpeed = 30f; //in degrees per second.
- public float pitchSpeed = 60f; //in degrees per second.
- // Update is called once per frame
- void FixedUpdate()
- {
- //convert targeting to local reference frame
- Vector3 localTarget = transform.InverseTransformDirection(targetDirection).normalized;
- //get the component angles
- float _yaw = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
- float _pitch = Mathf.Atan2(-localTarget.y, localTarget.z) * Mathf.Rad2Deg;
- //clam our rotations
- _yaw = Mathf.Clamp(_yaw, minYaw, maxYaw);
- _pitch = Mathf.Clamp(_pitch, minPitch, maxPitch);
- Debug.Log(_yaw);
- //turn them into quaternions.
- Quaternion desiredYaw = Quaternion.AngleAxis(_yaw, Vector2.up);
- Quaternion desiredPitch = Quaternion.AngleAxis(_pitch, Vector2.right);
- turret.localRotation = Quaternion.RotateTowards(turret.localRotation, desiredYaw, yawSpeed * Time.fixedDeltaTime);
- // gun.localRotation = Quaternion.RotateTowards(gun.localRotation, desiredPitch, pitchSpeed * Time.fixedDeltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement