LookAt doesnt work
So I want the object "TestEnemyCharacter" looking at my character as soon as a collision is triggered. However, whatever method I use to do it, the object always looks at a wrong direction that seems to be completely random. The object is also rotated, but I tried it without and it didnt change anything. I also think the properties of the collider that triggers the method doesnt play any role since im not turning the collider, but the object.
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
var target = GameObject.Find("Character");
GameObject.Find("TestEnemyCharacter").transform.LookAt(new Vector3(-target.transform.position.x, transform.position.y, -target.transform.position.z));
}
}
thank you for the help!
Answer by rereeser · Mar 29, 2021 at 12:18 AM
LookAt will turn the GameObject so that its Z axis points towards the given transform or worldspace vector. So in your case, you only need to feed it the player's transform:
GameObject.Find("TestEnemyCharacter").transform.LookAt(target.transform);
This would work as you wrote if you removed the negatives from the vector.x and .z (which would be equivalent to target.transform.position
).
Your answer
![](https://koobas.hobune.stream/wayback/20220613023426im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
rotate enemy toward player over time 0 Answers
Very strange tank turret behavior in Unity 3D 0 Answers
GameObject does not set transform 0 Answers
Difference of transform.forward and transform.lookAt 0 Answers
Transform.LookAt when enter trigger 0 Answers