Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question random range and enemy behavior script issues question

Discussion in 'Getting Started' started by lucasisrael813, Sep 5, 2023.

  1. lucasisrael813


    Nov 4, 2021
    Hi, i got a scipt attached to my enemy prefab and i want them to move independently from each other.
    Right now iam facing the issue, that Random Range generates the same numbers for all different emeny instances in my game at the same time even tho they get spawned at different times.

    Im not using any static keywords in my code. I also trief Random.Instances(System.Date.Time.Now.Seconds) but they wont stop walking the same way all at the same time.

    The only thing thats working is the Random.Range function call will generate a new number but its for all enemys at the same time which doesnt make any sense to me.

    Can anyone help me out here?

    using System.Collections;
    using System.Collections.Generic;
    using Unity.VisualScripting;
    using UnityEngine;
    using UnityEngine.UIElements;

    public class EnemxController : MonoBehaviour
    public float health;
    public float degreePerSec;
    public float moveSpeed;
    public float meterPerSec;
    Transform player;

    void Start()
    player = GameObject.Find("Player").GetComponent<Transform>();

    void Update()
    {//make the enemy rotate in circles
    transform.Rotate(Vector3.up, degreePerSec*Time.deltaTime);
    transform.position += transform.forward * (Time.deltaTime * meterPerSec);


    void MovetoPlayer()
    {//move in the direction of the player
    transform.position += (PlayerPosition() - transform.position) * (Time.deltaTime * moveSpeed);


    Vector3 PlayerPosition()
    //make the enemy go to a position around the the player
    return new (player.transform.position.x + Random.Range(-6,6f), player.transform.position.y,player.transform.position.z+ Random.Range(-6f, 6f));