- Home /
unity 2d C # script follows the enemy player.
hello.
I have this script for development in 3D. the enemy follows the player, but it does not work well for a 2d game. How do you change in 2d so that the enemy follows the player, using the x-axis?
script:
using UnityEngine;
using System.Collections;
public class EnemyInseguePlayer : MonoBehaviour {
public Transform player;
public float playerDistance;
public float rotationSpeed;
public float moveSpeed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
playerDistance = Vector3.Distance (player.position, transform.position);
if(playerDistance < 15f)
{
LookAtPlayer ();
}
if(playerDistance < 12f)
{
chase ();
}
}
void LookAtPlayer()
{
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
}
void chase()
{
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
}
}
Answer by DanGoldstein91 · May 21, 2015 at 06:30 AM
I recently coded an enemy to only chase the player on an X axis in Unity2D. My object has a rigid body, so I can get away with using this:
void chase() {
transform.position = Vector2.MoveTowards ( transform.position.x, player.position.x, moveSpeed * Time.deltaTime );
}
If it has to be restrained to the X-axis with code, try making a new Vector2 to specify the position, then plug that into the MoveTowards. Make sure your playerXAxis is being updated frequently. Just tested this and it works for me.
Vector2 playerXAxis;
playerXAxis = new vector 2 ( player.position.x, transform.position.y )
void chase() {
transform.position = Vector2.MoveTowards ( transform.position, playerXAxis );
}
Thank you for your reply.
I show you how I changed the scrip with your help.
I may have something wrong? I 3 errors
script:
using UnityEngine;
using System.Collections;
public class EnemyInseguePlayer : $$anonymous$$onoBehaviour {
public Transform player;
public float playerDistance;
public Vector2 playerXAxis;
public float moveSpeed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
playerDistance = Vector3.Distance (player.position, transform.position);
if(playerDistance < 15f)
{
LookAtPlayer ();
}
if(playerDistance < 12f)
{
chase ();
}
}
void LookAtPlayer()
{
Vector2 playerXAxis;
playerXAxis = new Vector2 ( player.position.x, transform.y );
}
void chase()
{
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
}
}
Hi, sorry.
playerXAxis = new Vector2 ( player.position.x, transform.y );
This should be transform.position.y. If you have any more errors please paste them in and let me know which lines the errors are on.
Answer by Awss · Sep 07, 2017 at 01:50 PM
I have found this code works, based on this video tutorial link text
using UnityEngine;
using System.Collections;
public class FollowAI : MonoBehaviour
{
public GameObject target; //the enemy's target
public float moveSpeed = 5; //move speed
void Start()
{
target = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
Vector3 targetDir = target.transform.position - transform.position;
float angle = Mathf.Atan2(targetDir.y,targetDir.x) * Mathf.Rad2Deg - 90f;
Quaternion q = Quaternion.AngleAxis(angle,Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation,q,180);
transform.Translate(Vector3.up * Time.deltaTime * moveSpeed);
}
}
I would like to inform you that this script works although when i drag in the player to the component or script and click play, the gameobject i dragged in disappears, i have to drag in the gameobject again, i'm not sure what the problem is but the script seems to work.
Answer by Lacheck · Apr 18, 2019 at 11:36 AM
If your view is not from above, but from side I suggest adding:
if(Vector3.Dot(transform.up, Vector3.down) > 0) { transform.Rotate(180f, 0f, 0f); }
Your answer
Follow this Question
Related Questions
Enemy backing away from player after going around corner? 1 Answer
Enemy is not stopping following the player 2 Answers
Enemy reorienting for player 1 Answer
Camera rotation around player while following. 6 Answers
Enemy to follow the Player 1 Answer