- Home /
Enemy AI Movement To A Target(Player) In A 2D Game
Hello everybody, this is my C# Script:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform Target;
private GameObject Enemy;
private GameObject Player;
private float Range;
public float Speed;
// Use this for initialization
void Start () {
Enemy = GameObject.FindGameObjectWithTag ("Enemy");
Player = GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame
void Update () {
Range = Vector2.Distance (Enemy.transform.position, Player.transform.position);
if (Range <= 15f) {
transform.Translate(Vector2.MoveTowards (Enemy.transform.position, Player.transform.position, Range) * Speed * Time.deltaTime);
}
}
}
Now, this script work for 3 seconds (in this time i move to two different directions whit the player) , but when i try to move in a third different direction the Enemy still goes in the previous direction! Is that a script problem? Help me please! [I'm italian, so dont speak strong english... use easy words :) ]
Likely your issues are with line 24. I'm not sure of the fix since I don't know your setup. You have to game objects, an enemy and a player, plus you have whatever this script is attached to. How is this object related to the other two, and what do you want to happen?
Answer by Domo23000 · Sep 18, 2014 at 11:02 PM
use this
Vector2 velocity = new Vector2((transform.position.x - player.position.x) * speed, (transform.position.y - player.position.y) * speed);
rigidbody2D.velocity = -velocity;
Vector2 velocity = new Vector2((transform.position.x - Player.transform.position.x) * Speed, (transform.position.y - Player.transform.position.y) * Speed);
GetComponent<Rigidbody2D>().velocity = -velocity;
@CORBY hmm..... where shoul you put it?
Vector2 velocity = new Vector2((transform.position.x - Player.transform.position.x) Speed, (transform.position.y - Player.transform.position.y) Speed); GetComponent().velocity = -velocity;
Answer by F-N · Feb 27, 2014 at 09:29 AM
Wy dont you make the enemy look at the player and then call: transform.position += transform.forward speed Time.deltaTime
Answer by AmarokStudios · Jul 22, 2014 at 12:28 PM
Can somebody translate this to Javascript? I'm not very good with C# and I'd like to understand this script better. I don't like copying and pasting scripts without knowing how to make adjustments to them. Thanks in advance!
#pragma strict
///Variables Declaration///
var target : Transform;
private var enemy : GameObject;
private var player : GameObject;
private var range : float;
var speed : float;
///Inizialization///
function Start ()
{
enemy = GameObject.FindGameObjectsWithTag("Enemy");
player = GameObject.FindGameObjectsWithTag("Player");
}
///Fixed Update///
function FixedUpdate ()
{
//Vector2.Distance = enemy.transform.position - player.transform.position
range = Vector2.Distance(enemy.transform.position, player.transform.position);
if(range <= 15f)
{
transform.Translate(Vector2.$$anonymous$$oveTowards(enemy.transform.position, player.transform.position, range) * speed * Time.deltaTime);
}
}
Translated but not checked!
Hope this will help you anyway!
Your answer
Follow this Question
Related Questions
Player movement boudaries in 2D 1 Answer
Making my 2d character's arm follow the mouse, scrpit not working, please help :c 1 Answer
Player stops moving upon collision 0 Answers
How can I move the character like 2D games? 1 Answer
How to limit the forces applied to an object to just forward, left, and right? 0 Answers