Hello everyone. I am having issues with LookAt not working on my instantiated gameobjects. It works fine if i just drop my enemy prefab into the scene but the instantiated prefabs do not look at their target. I am spawning enemies into the scene which are supposed to find a target, lookat/move towards target, and then attack the target. For some reason everything works except the lookat part. I feel like I am missing something very easy and stupid but I cant figure it out >_<.
I have tried instantiating with no position or rotation. I have tried transform.rotation and quaternion.identity and nothing seems to work. The enemy will spawn in, find a target, walk backwards toward the target and attack it while looking backwards still and this only happens on the instantiated enemies. The enemy prefab placed into the scene works fine.
Here is my spawner script:
Here is my moveto function in my enemy script which is called during update if it has a target:
What am i missing?
It kind of sounds like your enemy prefabs are looking backwards with respect to their local z axis (forward axis).
Note the docs: https://docs.unity3d.com/ScriptReference/Transform.LookAt.html
So if their forward direction is facing behind them, they’ll face the player backwards.
These are both the same prefab. The one closest to the screen was placed into the scene and the other was instantiated. They both have the same target and are walking to the left side of the screen. Their z axis both start out facing to the right but the one placed into the scene turns around to look at the target and the instantiated one doesnt.
Where or when is playerTarget actually set to a value? When you instantiate your enemy, external references to other scene objects can not be seved into a prefab. When you instantiate your enemy, you may want to set the playerTarget of that new enemy to your player object.
1 Like
So basically its a tower defense style game where enemy units and player units spawn at the same time on opposite sides of the map. I’m using an overlapsphere bigger than the map on each unit to add opposing units to a list of possible targets and then sets target to the closest possible target. In theory they are being spawned in and finding the closest target and moving to it. I’m a noob and may be doing this wrong? I don’t understand why it works perfectly fine on the placed prefab but not the instantiated. The lists and targets are public so I can see everything working correctly. The damn thing just wont look at the target lol.
Uhm, do you ever actually clear your playerTargets list? If not you’re constantly adding the same objects again and again to the list so it grows and grows and you have the same objects multiple times in the list. Of course once the objects are destroyed, they would be removed by your RemoveAll call. However the overall OverlapSphere thing seems to be completely unnecessary. The list of playerTargets is the same for all enemy units. So it’s pointless to have every enemy keeping the same list of objects and each one doing an OverlapSphere all the time.
It would make things much easier when you make the playerTargets list a static list of the PlayerUnit class and have each PlayerUnit add itself to the list in OnEnable and have it remove itself in OnDisable. That way every enemy Unit can access the same up-to-date list of player units. The same goes the other way round.
About your actual issue:
Make sure your gizmo mode in Unity is set to “local” and “pivot” to actually see the direction of “forward” when you select your object. Also, can you check which object the enemy actually referenced? Keep in mind that you can pause the game in the editor to explore the current state.
Ahhh good point on the list! I will have to switch to that system. Here is both units with gizmo set to pivot and local. They are both targetting the same playerbasefocus which is the white cube at the end of the map. Both facing opposite directions. The placed prefab has playerbasefocus in its list twice but there is only one playerbasefocus in the scene so they both definitely have the same target.
Omg i figured it out. My other unithealth script was doing something stupid! Sorry to waste your time and thank you for the list suggestion Bunny!