Patrol and Target Player Code?
I'm trying to make a simple patrol script that will also let the enemy target the player when the player goes within field of view, that's why I'm using a GameObject for the place to move next. I think I have it figured out but the enemy won't move. The enemy is using a circle collider with a Kinematic Rigidbody2D so it can float. This is a side-scrolling platformer. This is my code: (I am using the 2018.4 version of Unity)
public float speed = 10.0f;
private Vector2 position;
public GameObject target;
public GameObject PatrolL;
public GameObject PatrolR;
// Use this for initialization
void Start () {
target.transform.position = PatrolL.transform.position;
}
// Update is called once per frame
void Update () {
if (target.transform.position == PatrolL.transform.position)
{
transform.position = new Vector2(transform.position.x - (speed * Time.deltaTime), transform.position.y);
}
if (target.transform.position == PatrolR.transform.position)
{
transform.position = new Vector2(transform.position.x + (speed * Time.deltaTime), transform.position.y);
}
if (transform.position == PatrolL.transform.position && target.transform.position == PatrolL.transform.position)
{
target.transform.position = PatrolR.transform.position;
}
if (transform.position == PatrolR.transform.position && target.transform.position == PatrolR.transform.position)
{
target.transform.position = PatrolL.transform.position;
}
}
I've looked through multiple times and not found any glitches, can I get some help? Thanks
Comment
Your answer
Follow this Question
Related Questions
Centering camera between my player and a selected object. 0 Answers
AI Patrol 0 Answers
Can no1 help me??? 1 Answer
Script able to target two cameras? 0 Answers