- Home /
Enemy AI 2D not working? ( Rigidbody.Addforce() )
Having trouble getting the enemy ai to chase the character with rigidbody.addforce(). So far it seems to be sliding around the player like its on roller skates and eventually hitting it. I changed it from following using transform.position because I wanted it to also collide with walls. Ill leave my code below, if there's anything to fix it from sliding around I would greatly appreciate it. Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed = 10.0f;
public float diststop = 1.5f;
public GameObject player;
private Vector2 target;
private Vector2 position;
void Start()
{
target = new Vector2(0.0f, 0.0f);
position = gameObject.transform.position;
}
void Update()
{
position = gameObject.transform.position;
float dist = Vector2.Distance(target, position);
float x_pos = player.transform.position.x;
float y_pos = player.transform.position.y;
target = new Vector2(x_pos, y_pos);
float step = speed * Time.deltaTime;
// move sprite towards the target location
if (dist >= diststop)
{
//transform.position = Vector2.MoveTowards(transform.position, target, step);
GetComponent<Rigidbody2D>().AddForce((player.transform.position - transform.position) * speed * Time.smoothDeltaTime);
dist = Vector2.Distance(target, position);
}
}
}
you may want to change the velocity direction ins$$anonymous$$d of adding force. when you addforce it just adds to the game engines calculations and it figures to slow things down eventually. if you dont like what the phisics engine is doing for you, you can directly controll the movement yourself by changing the velocity every frame. when your object is not moving you can simply set the velocity to zero.
Answer by sean244 · Dec 28, 2018 at 06:35 AM
Try this...
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed = 10.0f;
public float diststop = 1.5f;
public Transform target;
private Rigidbody2D rb;
private Vector2 Position
{
get
{
return transform.position;
}
set
{
transform.position = value;
}
}
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
float dist = Vector2.Distance(Position, target.position);
float step = speed * Time.deltaTime;
if (dist >= diststop)
{
Position = Vector2.MoveTowards(Position, target.position, step);
rb.MovePosition(Position);
}
}
}
You’re welcome, but please make note of the changes I made: First of all, I cached the rigidbody. That way, you’re not calling ‘GetComponent’ at every frame - which is horrible for performance. Second of all, I call the rigidbody operation in ‘FixedUpdate’ rather than ‘Update’. This will ensure that it runs consistently across all devices, regardless of the frame rate. Third, I made properties out of ‘Target’ and ‘Position’. That way, you don’t have to constantly set their values in Update. Rather, they will return the values you need from them, only when they are accessed. I hope all of that helped. Please let me know if there’s anything you weren’t clear on.
Answer by giveson · Dec 30, 2018 at 09:54 PM
you could try: public transform transformofenemy;
void EnemyMove() // any method { transformofenemy.translate;(0, 0, 0) // x y and z }