Forked from andrew-raphael-lukasik/.UniversalTurretComponent.cs.md
Created
December 14, 2022 07:20
-
-
Save ArieLeo/adb44bbf98b1246a86bd82666c4ae52d to your computer and use it in GitHub Desktop.
basic turret with limited gimbal range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src* https://gist.github.com/andrew-raphael-lukasik/39977c56509eb60a8d44bd94e77cce7a | |
using UnityEngine; | |
public class MyTurretComponent : MonoBehaviour | |
{ | |
public Transform target; | |
[SerializeField] float | |
_reloadtime = 1f , | |
_launchForce = 10f; | |
[SerializeField] Vector2 | |
degreesPerSecond = new Vector2( 5 , 10 ) , | |
gimbalMin = new Vector2( -40 , -80 ) , | |
gimbalMax = new Vector2( 30 , 80 ); | |
void FixedUpdate () | |
{ | |
var parent = transform.parent; | |
float deltaTime = Time.fixedDeltaTime; | |
Vector3 srcLocalDegrees = transform.localEulerAngles; | |
Quaternion targetRotation = Quaternion.LookRotation( ( target.position - transform.position ).normalized , parent.up ); | |
Vector3 targetLocalDegrees = ( Quaternion.Inverse(parent.rotation) * targetRotation ).eulerAngles; | |
Vector3 dstLocalDegrees = new Vector3( | |
targetLocalDegrees.x>180 | |
? Mathf.Max( targetLocalDegrees.x , 360f+gimbalMin.x ) | |
: Mathf.Min( targetLocalDegrees.x , gimbalMax.x ) , | |
targetLocalDegrees.y>180 | |
? Mathf.Max( targetLocalDegrees.y , 360f+gimbalMin.y ) | |
: Mathf.Min( targetLocalDegrees.y , gimbalMax.y ) , | |
targetLocalDegrees.z | |
); | |
transform.localEulerAngles = new Vector3( | |
Mathf.MoveTowardsAngle( srcLocalDegrees.x , dstLocalDegrees.x , degreesPerSecond.x * deltaTime ) , | |
Mathf.MoveTowardsAngle( srcLocalDegrees.y , dstLocalDegrees.y , degreesPerSecond.y * deltaTime ) , | |
srcLocalDegrees.z | |
); | |
#if UNITY_EDITOR | |
Debug.DrawRay( transform.position , parent.rotation*Quaternion.Euler(targetLocalDegrees)*Vector3.forward , Color.cyan ); | |
Debug.DrawRay( transform.position , parent.rotation*Quaternion.Euler(dstLocalDegrees)*Vector3.forward , Color.white*0.5f ); | |
#endif | |
} | |
#if UNITY_EDITOR | |
void OnDrawGizmos () | |
{ | |
var parent = transform.parent; | |
if( parent!=null ) | |
{ | |
Vector3 position = transform.position; | |
Vector3 forward = transform.forward; | |
Vector3 parentRight = parent.right; | |
Vector3 parentUp = parent.up; | |
Vector3 parentForward = parent.forward; | |
UnityEditor.Handles.color = Color.red; | |
Vector3 xArcDir0 = Quaternion.AngleAxis(gimbalMin.x,parentRight) * parentForward; | |
Vector3 xArcDir1 = Quaternion.AngleAxis(gimbalMax.x,parentRight) * parentForward; | |
UnityEditor.Handles.DrawWireArc( position , parentRight , xArcDir0 , gimbalMax.x - gimbalMin.x , 1 ); | |
Gizmos.color = Color.red; | |
Gizmos.DrawLine( position , position+xArcDir0 ); | |
Gizmos.DrawLine( position , position+xArcDir1 ); | |
UnityEditor.Handles.color = Color.green; | |
Vector3 yArcDir0 = Quaternion.AngleAxis(gimbalMin.y,parentUp) * parentForward; | |
Vector3 yArcDir1 = Quaternion.AngleAxis(gimbalMax.y,parentUp) * parentForward; | |
UnityEditor.Handles.DrawWireArc( position , parentUp , yArcDir0 , gimbalMax.y - gimbalMin.y , 1 ); | |
Gizmos.color = Color.green; | |
Gizmos.DrawLine( position , position+yArcDir0 ); | |
Gizmos.DrawLine( position , position+yArcDir1 ); | |
Gizmos.color = Color.blue; | |
Gizmos.DrawRay( position , parentForward ); | |
Gizmos.color = Color.white; | |
Gizmos.DrawRay( position , forward ); | |
} | |
} | |
void OnDrawGizmosSelected () | |
{ | |
var label = new System.Text.StringBuilder(); | |
if( transform.parent==null ) label.Append("! NO PARENT TRANSFORM !\n"); | |
Vector3 localEulerAngles = transform.localEulerAngles; | |
label.AppendFormat( System.Globalization.CultureInfo.InvariantCulture , "( {0:0.0} , {1:0.0} , {2:0.0} )" , localEulerAngles.x , localEulerAngles.y , localEulerAngles.z ); | |
UnityEditor.Handles.Label( transform.position , label.ToString() ); | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment