x


look at target lateral boat

hey fokes, can anyone tell me if using this:

var LookAtTarget:Transform;

function Update () { transform.LookAt(LookAtTarget); }

I can make the boat look at me, but the boat directs the front of the boat into me, so my question is, how can i make the boat look at me with his lateral part, where the cannons are, so that after this he can shoot at me.

more ▼

asked May 18 '12 at 11:40 AM

ruifernandes01 gravatar image

ruifernandes01
23 5 9 14

Do this:

transform.LookAt(LookAtTarget);
transform.Rotate(90, Vector3.up);

At some point you will have to make your movement script significantly more complex, but for now this will do.

May 18 '12 at 11:47 AM syclamoth

A little correction:

transform.Rotate(90 * Vector3.up);
May 18 '12 at 12:00 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

LookAt aligns transform.forward to the target. If you want to align any other local direction, use FromToRotation:

function Update(){
  var dir = LookAtTarget.position - transform.position;
  dir.y = 0; // use this to keep the direction strictly horizontal
  // rotate left side (-right) in direction to the target:
  transform.rotation = Quaternion.FromToRotation(-transform.right, dir);
}

If you want to slowly rotate the enemy ship towards you, use Slerp:

function Update(){
  var dir = LookAtTarget.position - transform.position;
  dir.y = 0; // use this to keep the direction strictly horizontal
  // gradually rotate left side (-right) in direction to the target:
  transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.FromToRotation(-transform.right, dir), 2.5 * Time.deltaTime); // 2.5 is the speed - adjust it if necessary
}
more ▼

answered May 18 '12 at 11:58 AM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x34
x7

asked: May 18 '12 at 11:40 AM

Seen: 290 times

Last Updated: May 18 '12 at 12:05 PM