0
\$\begingroup\$

I am new to Unity. I have searched but I couldn't find any answers on this. Here is my code:

somethingPosition = new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z);
transform.LookAt(somethingPosition);

The somethingPosition only allows rotation of the Y axis so. Anyways, I have this script on the enemy AI.

The issue is this: the enemy looks at the player but looks to the left of the player. The same issue occurs when I have my follow code happen:

transform.position = Vector3.MoveTowards(transform.position, player.position, speed * Time.deltaTime);

The enemy will continue going to the left of the player but will not hit the player. What are some suggestions you guys can survive? Thanks!

I am aswell using a NavMesh and NavMeshAgent

\$\endgroup\$
1
\$\begingroup\$

Double check that your model of the character does not have an offset. If it does, the object's centre would be away from the model, which would cause all calculations (including LookAt) to point to that place. If you are using NavMeshAgents, there's a chance you are doing collisions based on the mesh, creating a bounding box around it, which would explain why everything else seems to be working.

\$\endgroup\$
  • \$\begingroup\$ My character does not have an offset and I am using a NavMeshAgent too \$\endgroup\$ – Captain Caboose Jan 9 at 16:43
  • \$\begingroup\$ @CaptainCaboose Try creating a new cube, and add is as a direct child to player, reset all of its transformation so it has position (0, 0, 0). Make the cube small, so it represents a point. This is the center of your player. Now try to run the game again, and check where the enemy is looking at. Depending on the result, I may be able to add more details to my answer. It may be helpful if you add screenshots in your original question. \$\endgroup\$ – TomTsagk Jan 9 at 16:48
1
\$\begingroup\$

Firstly, you have set target y value to look at to be transform.position.y. This can be okay or not depending on if you are getting the AI to look straight forward.

Secondly, different 3d modeling softwares have either a z-up or a y-up configuration. Check axis of player and AI.

Thirdly, for good results take an empty axes and child it to your player at position where you want your AI to lookat.

Also, as TomTsagk pointed out, your player origin might not be within its mesh and check colliders.

good Luck.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.