my destination change doesn't move from randomx and random z can anyone help??

here my code

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

public class DestinationChange : MonoBehaviour
{
public int xPos;
public int zPos;

void OnTriggerEnter(Collider other)
{
    if (other.tag == "Enemy")
    {
        xPos = Random.Range(64, 431);
        zPos = Random.Range(45, 442);
        this.gameObject.transform.position = new Vector3(xPos, 1.5f, zPos);
    }
}

}
,this my script

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

public class DestinationChange : MonoBehaviour
{
public int xPos;
public int zPos;

void OnTriggerEnter(Collider other)
{
    if (other.tag == "Enemy")
    {
        xPos = Random.Range(64, 431);
        zPos = Random.Range(45, 442);
        this.gameObject.transform.position = new Vector3(xPos, 1.5f, zPos);
    }
}

}

1 Answer

1

Use a breakpoint to make sure that your code is being hit.

Possible reasons it wouldn’t be working:

  • The DestinationChange component isn’t attached to your player object
  • The collider for the enemy objects is attached to a game object that doesn’t have the tag “Enemy”
  • The collider for your player doesn’t have IsTrigger set to true. If you don’t want that to be true, use OnCollisionEnter instead of OnTriggerEnter.