Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class TurretController : MonoBehaviour {
- public ShootingPoint shooter;
- public Transform pitchPivot;
- public Transform yawPivot;
- public float pitchTurnSpeed = 25;
- public float yawTurnSpeed = 25;
- public float aimAhead = 1;
- Vector3 lastTargetPosition = Vector3.zero;
- Transform _target;
- public Transform Target {
- get { return _target; }
- set {
- Transform old = _target;
- _target = value;
- if(old != Target && Target)
- lastTargetPosition = Target.position;
- }
- }
- public Vector3 AimPosition { get; private set; }
- public float yawSpeed;
- public float pitchSpeed;
- float TargetPitch {
- get {
- return Target ? Vector3.Dot(Vector3.Cross(pitchPivot.forward, (AimPosition - pitchPivot.position).normalized), pitchPivot.right) : 0;
- }
- }
- float TargetYaw {
- get {
- return Target ? Vector3.Dot(Vector3.Cross(yawPivot.forward, (AimPosition - yawPivot.position).normalized), yawPivot.up) : 0;
- }
- }
- void Update() {
- if(Target) {
- yawPivot.localRotation = yawPivot.localRotation * Quaternion.Euler(0, TargetYaw * yawTurnSpeed * Time.deltaTime, 0);
- pitchPivot.localRotation = pitchPivot.localRotation * Quaternion.Euler(TargetPitch * pitchTurnSpeed * Time.deltaTime, 0, 0);
- if(shooter.IsReady)
- shooter.Shoot();
- }
- }
- void LateUpdate() {
- if(Target) {
- AimPosition = Target.position + (Target.position - lastTargetPosition) * aimAhead;
- lastTargetPosition = Target.position;
- }
- }
- void OnDrawGizmos() {
- if(Target) {
- Gizmos.color = Color.red;
- Gizmos.DrawLine(shooter.transform.position, AimPosition);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment