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. Dismiss Notice

Question How do i find transform of player in a prefab of my enemy

Discussion in 'Prefabs' started by Fusible_YT, Jul 2, 2023.

  1. Fusible_YT

    Fusible_YT

    Joined:
    Jun 28, 2023
    Posts:
    1
    so I'm very new to unity and I'm making this top down shooter and I'm trying to write this script where the enemy follows the player and it works but I'm trying to make it a prefab so I can spawn infinite of them and whenever I do this I can't put in my transform of my player because it isn't in the scene so my enemy doesn't follow the player cause it doesn't know where it is can someone please help me here's my code its probably bad

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class EnemyScript : MonoBehaviour
    {
    public float moveSpeed = 5f;
    public Transform Player;
    private Rigidbody2D rb;
    private Vector2 Movement;
    // Start is called before the first frame update
    void Start()
    {
    rb = this.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
    Vector3 direction = Player.position - transform.position;
    float angle = MathF.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(0, 0, angle + 90);
    rb.rotation = angle;
    direction.Normalize();
    Movement = direction;
    }
    private void FixedUpdate()
    {
    moveCharactor(Movement);
    }
    void moveCharactor(Vector2 direction)
    {
    rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    }
    }
     
  2. bloodthirst69

    bloodthirst69

    Joined:
    Oct 1, 2017
    Posts:
    28
    Somewhere in your code there must be a place where you call "Instantiate" on this prefab to spawn it, right ? It's there where you need to setup your reference to the player.
    Naturally, the enemy spawner itself should be in the scene therefore you can assign it the player transform, and every time the spawner spawns an enemy , it can assign the player to it.
    I'll keep it very short here but this should get the idea across , just place a gameobject in the scene and attach this SpawnerExample component to it, then do the spawning through the Spawn method

    Code (CSharp):
    1. public class SpawnerExample : MonoBehaviour
    2. {
    3. // Assign the enemy prefab
    4. [SerializeField]
    5. private EnemyScript enemyPrefab;
    6.  
    7. // Assign the player's transform in the scene
    8. [SerializeField]
    9. private Transform playerRef;
    10.  
    11. public  EnemyScript Spawn()
    12. {
    13. // spawn a new enemy
    14. EnemyScript newEnemy = Instantiate(enemyPrefab);
    15.  
    16. // assign the player transform to it
    17. newEnemy.player = playerRef;
    18.  
    19. // return it , the new enemy will have the player reference correctly
    20. return newEnemy;
    21. }
    22. }
     
  3. itslexismith18

    itslexismith18

    Joined:
    Jul 4, 2023
    Posts:
    2
    In this updated script, the enemy finds the player object using a tag called "Player" in the Awake() method. Make sure to assign the "Player" tag to your player object in the Unity editor. With this modification, you can now instantiate the enemy prefab, and it will correctly follow the player wherever it is in the scene.