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
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);
}
almo
2