- Home /
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);
Your answer
Follow this Question
Related Questions
Problem Solved [JS] 1 Answer
Detect if game object is near a specific tag 1 Answer
Hexagon-Grid Distance 2 Answers
variable++ error... 1 Answer