Unity 2d - Enemy following player [closed]

0

Well, I'm having trouble making the enemy follow the player, I made the code below:

public class folow : MonoBehaviour {

public Transform target;//set target from inspector instead of looking in Update
public float speed = 3f;
private Transform myTransform;


void Start()
{
    myTransform = this.GetComponent<Transform>();

}

void Update()
{

    //rotate to look at the player
    transform.LookAt(target.position);
    transform.Rotate(new Vector3(0, -90, 0), Space.Self);//correcting the original rotation



        myTransform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);



}

The problem is that it just tries to go towards the player, but can not dodge objects and gets stuck in any obstacle

    
asked by anonymous 07.03.2017 / 16:43

1 answer

1

Live,

Basically your code is correct ... The now you lack intelligence to find the possible way to reach the target you scored. It simply has:

Both are great choices ... Just take into account that in 2d only the A * Pathfinding Project is compatible. It could give an example, but the necessary code is practically null, since already is done all the scripts that needs. (In both of the options I mentioned above)

    
08.03.2017 / 18:55