x


Aim a transform at a point along a vector

Hi everyone,

I have been pulling my hair out trying to find a solution to this problem, and I don't have much hair left..

I have two transforms, representing guns on a craft, that fire along their Z-axis. Both are at the end of 'wings', and so need to have an aim-point to converge upon. This aim point is, by default, an arbitrary position along the Z-axis of another transform pointing out of the nose of the craft (transform.forward * 1000).

The issue is that it is very rare a target is at that distance, so I need to 'focus' the fire at an appropriate distance to the target being shot at, whilst leaving the '2D' aiming to the player.

I have a routine that scans all GO's in the scene, filtering them down to the one presumably being shot at using the dot product of the target from the Z-axis of the transform on the craft's nose, so I've got details of the closes object to the cross hair, including its distance.

How do I now get the weapon transforms to aim at a point along the Z-axis of the craft's nose, at the known distance of the target being shot at so that the shots converge on the target successfully if the player is shooting accurately?

var fwd = targetPointer.TransformDirection (Vector3.forward);

When I set the aimpoint to fwd*distanceToTarget strange stuff happens, as if the fwd vector is working in World Space and not maintaining the craft's local Z-axis as it rotates.

The distance to target is set by:

targetDistance = Vector3.Distance(targetPointer.position, targetTransform.position);
more ▼

asked Jul 30 '12 at 04:47 PM

nockieboy gravatar image

nockieboy
3 2 4

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
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:

x355
x198

asked: Jul 30 '12 at 04:47 PM

Seen: 335 times

Last Updated: Jul 30 '12 at 04:47 PM