- Home /
cannon aim problem
********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
}
}
}
This might have something to do with how 3D $$anonymous$$ax and Unity sees axis. A lot of 3D programs does not agree upon whats Y and whats Z.
Yeah, look at your cannon-body, and make sure that the green y axis is in fact pointing up.
You might also try using Transform.Rotate(), and play with the second parameter (local space vs. world space)
Answer by brightlance · Mar 25, 2012 at 09:55 PM
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.
Answer by CreativeStorm · Mar 25, 2012 at 10:29 PM
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 ;)
yaay! it worked.. but still i cant make the cannon to rotate with the body.. when i try putting the cannon as a child of the body it goes nuts and starts to avoid me XD
Answer by marcuscodes · Mar 27, 2012 at 05:25 PM
hmmm.. it still don't work :(
is this code right?
var rotation = Quaternion.LookRotation(target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 2);
and if you want to lock axels you just write rotation.x = 0; infront of the transform.rotation
Answer by CreativeStorm · Mar 27, 2012 at 05:51 PM
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.
var targetRotation : Quaternion;
if(GunTarget != Vector3.zero)
{
targetRotation = Quaternion.LookRotation(GunTarget - thisTransform.position);
MyTurret.rotation = Quaternion.Slerp(MyTurret.rotation, targetRotation, 9* Time.deltaTime);
}
else
{
targetRotation = thisTransform.rotation;
MyTurret.rotation = Quaternion.Slerp(MyTurret.rotation, targetRotation, 1* Time.deltaTime);
}
MyTurret.eulerAngles.x = 0;
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
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
making one object a parent of another via javascript 2 Answers
character and Camera rotate 2 Answers
rotate object to touch position 1 Answer