Scenario: A tank drives along an incline steep enough that the tank is nearly about to barrel roll into the ditch. The commander sees an enemy. The gunner first turns the turret to face the target as best he can but this is made difficult by the slope, he then compensates using the elevation of the gun to strike the enemy.
I am trying to create an AI tank with a turret mounted on top. But this is more difficult than just having a car and sticking the FPS tutorial’s sentry gun on the roof
The sentry gun operates like a ball and socket joint, however I am looking for more the combination of swivels like on a camera’s tripod, a swivel for full 360deg rotation about one axis and another hinge with 30deg of freedom up or down.
There is more to this than just using “turret.position-target.position”…“quaternion”… “Lookat” and then setting one of Lookat’s variables back to 0. This results in ridiculus looking behaviour, with the turret staying level with the world while the rest of the vehicle turns, climbs hill, falls off cliff, rolls into the ditch.
The desired effect is that the turret will be constrained relative to its parent rigidbody while still trying its best to aim at a target.
I dont care how this is achieved, if by workarounds or something clever. I dont mind if its not smoothed or lerped or slerped I would work on that later.
Here is some code I am using, byond the first /* it is all disabled stuff which I gathered from forums/answers/scriptRefrence because I thought it looked promising but didn’t work. Though I didn’t want to throw any away because it took so long to gather and might still be usefull.
^^^^MY FIRST UNITY ANSWERS QUESTION :-)^^^^^
(btw I cant seems to attach an image it just says nothing chosen after I click open)
var thisTransform : Transform; var target : Transform; var a : boolean; function Start() { thisTransform = transform; } function Update() { if(a) thisTransform.LookAt(Vector3(target.position.x,target.position.y,target.position.z)); else thisTransform.LookAt(Vector3(target.position.x,thisTransform.position.y,target.position.z)); } [1]: http://