- Home /
Rotate a ship so cannons can face the objective (another ship)
Hello! I've tried a lot of examples of rotation and I didn't have the expected results. So, here it goes:
I have a ship (parent) and this ship has a cannon (child). The cannon is located at the left side of the ship, their x axis is faced to the orange arrow (as you can see in the example picture). How do I rotate the ships in a way that the cannons face the objective (another ship)? I don't know how to get the correct rotation angle...
I hope you could help me...
=)
The example pic: example pic
Answer by aldonaletto · Jul 14, 2011 at 09:36 PM
You can create a rotation from your cannon to the target, then apply it to the whole ship. This script must be attached to the ship, and you must assign the target transform to the target variable:
var target: Transform; // your target
var speed: float = 2; // turning speed
function Update(){
var dir = target.position - transform.position; // target direction
// generate rotation from "transform.left" (doesn't exist) to the target
var rot = Quaternion.FromToRotation(-transform.right, dir);
// smoothly turns the ship to the target
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * speed);
}
Hello aldonaletto ! Your tip worked for me! Thanks, thanks, thanks a lot! =)
s$$anonymous$$lthcold.. if aldonaletto answered your question you should ACCEPT it.
Answer by almo · Jul 14, 2011 at 08:16 PM
Vector3.RotateTowards any help?
http://unity3d.com/support/documentation/ScriptReference/Vector3.RotateTowards.html
First of all thanks for the reply! =)
Well, it rotates.. but the cannon still pointing to the other side... According to the example picture, now the blue arrow is pointing to the target...But I need the orange arrow pointing to the target.. There's a way ?
If the cannon is an object, can't you use RotateTowards to rotate the cannon towards the player?
But I have a old pirate ship and the cannons are fixed... =( I should had made a modern ship... damn!
Your answer