Quantcast
Channel: Answers for "Turret Rotation on a moving object with rotation limits"
Viewing all articles
Browse latest Browse all 6

Answer by robertbu

$
0
0
Here is a script that gets you most of the way there. There some unknowns in your question, so I expect you'll have a few changes, but I believe all the heavy lifting is done. #pragma strict var target : Transform; var gun : Transform; var turretDegreesPerSecond : float = 45.0; var gunDegreesPerSecond : float = 45.0; var maxGunAngle = 45.0; var maxTurretAngle = 45.0; private var qGunStart : Quaternion; private var trans : Transform; function Start() { trans = transform; qGunStart = gun.transform.localRotation; } function Update () { var distanceToPlane = Vector3.Dot(trans.up, target.position - trans.position); var planePoint = target.position - trans.up * distanceToPlane; var qTurret : Quaternion = Quaternion.LookRotation(planePoint - trans.position, transform.up); var qForward : Quaternion = Quaternion.LookRotation(transform.parent.forward); if (Quaternion.Angle(qTurret, qForward) < maxTurretAngle) { transform.rotation = Quaternion.RotateTowards(transform.rotation, qTurret, turretDegreesPerSecond * Time.deltaTime); } else { Debug.Log("Target beyond turret range"); } var v3 = Vector3(0.0, distanceToPlane, (planePoint - transform.position).magnitude); var qGun : Quaternion = Quaternion.LookRotation(v3); if (Quaternion.Angle(qGunStart, qGun) <= maxGunAngle) gun.localRotation = Quaternion.RotateTowards(gun.localRotation, qGun, gunDegreesPerSecond * Time.deltaTime); else Debug.Log("Target beyond gun range"); } And here is a unitypackage with this script in use: [www.laughingloops.com/Turret2.unitypackage][1] Note that maxGunAngle() will define the maximum delta from the start position of the gun. For maxTurretAngle, the maximum angle is between the forward of the parent object and the gun. [1]: http://www.laughingloops.com/Turret2.unitypackage

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>