0
\$\begingroup\$

How to make an object look at the target direction not at the target himself ?

public Transform target;

void Update() {

    transform.LookAt(target);
    float step = 2 * Time.deltaTime;
    transform.position = Vector3.MoveTowards(transform.position, target.position, step);


}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ What do you mean by "target direction"? A specific point in space? The direction that target is looking at (how do you define that?)? \$\endgroup\$ Commented Mar 28, 2017 at 7:02
  • 1
    \$\begingroup\$ I mean "The direction that target is looking at" \$\endgroup\$
    – user43474
    Commented Mar 28, 2017 at 7:04
  • \$\begingroup\$ You can't really "look at a direction" - you need to pick a point on the direction vector to look at. (Maybe your question is how to calculate a direction vector and pick a point on it? In that case you should update the question accordingly) \$\endgroup\$ Commented Mar 28, 2017 at 7:31

1 Answer 1

1
\$\begingroup\$

If you want one object to look parallel to the sight-direction of another object, you can simply turn it into the same rotation:

this.transform.rotation = target.transform.rotation;

But if you want one object to look at whatever thing another object is looking at, you first need to figure out where that other thing is, because the direction would differ depending on if it's something directly in front of the other object or something at the horizon.

One option is to do a raycast from the other object to find the point where its line of sight is broken and then use LookAt for that point.

RaycastHit hit;
if (Physics.Raycast(target.transform.position, target.transform.rotation.eulerAngles, out hit)) {
     this.transform.LookAt(hit.point);
}

Note that Physics.Raycast is only broken by objects with colliders.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .