Solved Thanks!
Question: how to get a good looking cannon with the body only moving in y axis and cannon moving in x axis.
Problem: Unity and 3ds max had problem with x,y,z.
Fix: Create parents to the parts and make the parent rotate.
here is the code i used. the cannon is only an illusion that its only moves on the x axis:
var target:Transform;
var cannon = false;
var body = false;
function Update () {
if (target) { // end early if no target
if (Vector3.Distance(transform.position, target.position) > 20) { // if target is to far start rotate like a radar
transform.Rotate(Vector3.up * (Time.deltaTime *100), Space.World);
} else { // else set new rotation depending on what object the script is set on
var new_rotation = Quaternion.LookRotation(target.position - transform.position);
if (cannon) {
// if you want it to look good and same time work don't lock any axis here!
}
if (body) {
new_rotation.x = 0; // lock x
new_rotation.z = 0; // lock z
}
transform.rotation = Quaternion.Slerp(transform.rotation, new_rotation, Time.deltaTime * 2); // smoot look at
}
}
}
I’ve recently done a similar thing in my project. In order to get the separate parts to rotate correctly, first rotate the body look at the target’s position like you are already doing, but make it look at Vector3(target.x,body.y,target.z).
Doing this will stop the gun from aiming in weird angles.
I had a very similar problem with my tank. The turret rotates automatically toward the given target and fires. So my turret was pointing upwards and the top was pointing towards the target. Its related to the axis differences between Unity and Max. I Just added the turret to an empty GO as a child. Now i rotate the Parent and everything is fine. For me this was the fastest solution - the are better ones for sure
well for my tank i use two target … kinda of - not right sure how to explain. When there is a target, it is the target. In other situations the target for the rotation is set to the rotation of the body… so it smoothly rotates to where it’s supposed to at all times.
this is the code for my target its in an function called from the update function.
The Turret is cached in “myTurret”.
“GunTarget” is a Vector3 position set for my by touching the screen - would be your player’s position for you i assume - sry, i’m in a hurry. Hope i could help though