Constrain Turret Rotation while child of Rigidbody

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://

From your description of how you want it to work, it sounds like you’ll probably want to use a bunch of simple rotations with appropriately parented objects.

What I’m imagining is the following:

Tank Body → Turret → Gun barrel

In your tank’s hierarchy, meaning that the tank is the ultimate parent. This way, you’ll move your tank and the turret and gun barrel will move properly in relation to your main body. Handling the aiming will come down to rotating the turret (within whatever constraints you have) and then rotating the gun barrel (within your constraints) with the appropriate inputs. Transform.RotateAround() is a good function for this kind of thing.

This will take care of the tank if it’s to be controlled by the player but if you’re trying to create AI, you’ll have to come up with some sort of best-solution-target-acquiring code to make it work.

Hope that’s clear/it helps…

Aha I have found the answer here. This script can be easily modified and constrained.