Hello!
I’m new using Unity 5.0 and I started c# recently. I’ve created a small script for the enemys in my game who allows them to follow the player. But I want to increase their speed, adding a speed variable. I want to keep quite the same script. Here is the script :
using UnityEngine;
using System.Collections;
public class EnemyIA : MonoBehaviour {
enum dir
{
LEFT,
RIGHT
};
GameObject Playerbase;
public int life = 3;
dir direction = dir.LEFT;
// Use this for initialization
void Start ()
{
Playerbase = GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame
void Update ()
{
if (Mathf.Abs (transform.position.x - Playerbase.transform.position.x) > 3.5f)
return;
dir tmp = direction;
transform.position = Vector3.MoveTowards (transform.position, Playerbase.transform.position, 0.01f);
if (transform.position.x - Playerbase.transform.position.x > 0)
direction = dir.LEFT;
else
direction = dir.RIGHT;
if (tmp != direction)
{
transform.Rotate(0, 180, 0);
}
}
Sorry for the mistakes I’m french. Thank you.