how to have a game object face its target ?
I have a game object, a pig in fact, which is looking for mushrooms using Raycast. While searching, the pig changes its direction every 2 seconds. When it raycasts a target, it goes to it :
this.transform.position = Vector3.MoveTowards(this.transform.position, mushPosition, speedPig * Time.deltaTime);
But as the pig is not a point, and as the Character Controller is a the center of the Pig, it may not be "facing" its target. I've been trying to resolve that dummy question all day long, but in vain. I get its rotation value at the moment it finds the mushroom using this.transform.rotation.y but what next ? Can't figure the math...
So if anyone can help a newbie trying to help pigs eating mushrooms... it would be nice.
Thanks in advance
Answer by jdean300 · Aug 28, 2016 at 11:04 PM
https://docs.unity3d.com/ScriptReference/Transform.LookAt.html
this.transform.LookAt(mushPosition);
Your answer
![](https://koobas.hobune.stream/wayback/20220612091742im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 3d rotate gameobject in touch direction over an axis 0 Answers
Change pivot point of gameobject 5 Answers
Check rotation of an obstacle 0 Answers
Gun-recoil problem C# 0 Answers
Error with Instantiate - Object rotated incorrectly 0 Answers