Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Instantiated enemies will not LookAt target...?

Discussion in 'Scripting' started by drift916, Jan 10, 2023.

  1. drift916

    drift916

    Joined:
    Nov 14, 2022
    Posts:
    5
    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:
    upload_2023-1-9_16-58-1.png

    Here is my moveto function in my enemy script which is called during update if it has a target:

    upload_2023-1-9_16-58-53.png

    What am i missing?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    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.
     
  3. drift916

    drift916

    Joined:
    Nov 14, 2022
    Posts:
    5
    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.

    upload_2023-1-9_18-23-13.png

    upload_2023-1-9_18-23-55.png
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    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.
     
    angrypenguin likes this.
  5. drift916

    drift916

    Joined:
    Nov 14, 2022
    Posts:
    5
    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.

    upload_2023-1-9_18-49-0.png

    upload_2023-1-9_18-50-3.png
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    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.
     
  7. drift916

    drift916

    Joined:
    Nov 14, 2022
    Posts:
    5
    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.

    upload_2023-1-10_5-17-48.png

    upload_2023-1-10_5-18-11.png
     
  8. drift916

    drift916

    Joined:
    Nov 14, 2022
    Posts:
    5
    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!