Question by
CristiBala24 · Sep 12, 2016 at 01:45 PM ·
game development
Help with Time still for enemy AI patrol
So I have a script and i want to add it to when it gets to a point the enemy should wait a little. Here is the code:
using UnityEngine; using System.Collections;
public class EnemyPatrolAI : MonoBehaviour {
float startingPos; float endPos;
public float moveSpeed = 1f;
public int unitsToMove = 5;
public int velocity = 1;
public Transform transform;
public Animator anim;
public float timeStill = 2f;
public bool facingRight;
void Start()
{
}
void Awake() { startingPos = transform.position.x; endPos = startingPos + unitsToMove; }
public void FixedUpdate()
{
if (facingRight)
{
GetComponent<Rigidbody2D>().velocity += new Vector2 (moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
if (GetComponent<Rigidbody2D>().position.x >= endPos)
{
facingRight = false;
if (facingRight == false)
{
transform.localScale = new Vector2(-1, transform.localScale.y);
anim.SetBool ("moving", true);
}
}
if (!facingRight)
{
GetComponent<Rigidbody2D>().velocity -= new Vector2 (moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
if (GetComponent<Rigidbody2D>().position.x <= startingPos)
{
facingRight = true;
if (facingRight == true)
{
transform.localScale = new Vector2(1, transform.localScale.y);
anim.SetBool ("moving", true);
}
}
if (GetComponent<Rigidbody2D>().velocity.magnitude > moveSpeed)
{
GetComponent<Rigidbody2D>().velocity = GetComponent<Rigidbody2D>().velocity.normalized * moveSpeed;
}
}
}
Thanks for help!
Comment