- Home /
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://
Just a note on the code that you've posted - most people will see that wall-o-text and run the other direction. If you're not using most of it, don't post that stuff.
Well sounds worth a try to get more answers, it might scare some away, but I suspect that if they are intimidated by some commented out code they don't know any more than me anyways :)
Answer by RetepTrun · Jun 05, 2011 at 02:22 AM
Aha I have found the answer here. This script can be easily modified and constrained.
http://answers.unity3d.com/questions/125748/leftright-updwn-of-me-detection.html#answer-126251
Yeah, you can definitely use this to have one part rotate left/right and one part up/down (like a turret).
Answer by Chris D · Jun 02, 2011 at 10:51 PM
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...
Yup thats my heirarchy, and I'll look into RotateAround() to see if it could be usefull.
Your thinking is the same as $$anonymous$$e was when I started and it works just fine with transform.rotate but once I try using lookat, quaternion, and more fancy stuff, these are difficult to constrain and seem to ignor the rotation of a heirarchy parent.
Simply choosing a good heirarchy means that turret moves with the vehicle but in the funny way I was trying to describe. The tricky part that I can't get is constraining things it to parent-related limits rather than a world-related limits. Like I have successfully kept the turret rotating 360 about 1 axis only, but this unfortunately this has also meant that it is permanently level with the world axis, so even if the tank body is turned upside down, the turret is still rightside up.
To limit the rotation relative the parent while using transform.lookat probably needs some vector3 math so I think maybe I should make my own targeting from scratch so I can stick with the easy transform.rotate . I have been thinking of just using something that rotates left for 2 deg/frame if the target is to the left, otherwise go right 2 deg/frame, likewise for the barrel go clockwise about x if target is up, otherwise counterclockwise. It works fine for the lego NXT set making a car follow a line. The difficulty is how to detect if the enemy is left/right, up/down of me. Any ideas anyone?
Your answer
Follow this Question
Related Questions
How to rotate a rigidbody without its attached anchor rigid body flying off? 0 Answers
Rotate rigidbody using MoveRotation() around the global y axis 2 Answers
Rotating 2D Sprite on 1 Axis Using 2 Input Axes 0 Answers
how can I rotate object reading from excel data & time steps 3 Answers
Rotating a rigid body to face left or right smoothly 4 Answers