So this causes the enemy to move towards me, but the problem is the enemy doesn’t head straight at me. Even if the enemy is right in front of me, it moves in a circular orbit around the player, slowly getting closer and closer. And if u try to increase the speed, the orbit just becomes more elliptical. How can i make the enemy come straight at me, instead of circling me?
It sounds like you not setting the angular velocity… Here’s a typical rigidbody movement motor… Assuming you have 2 vectors, point a the position of the game object, b the position of the target to move to…
// Make the character rotate towards the target rotation
var turnDir = nextDirection;
if (turnDir == Vector3.zero)
{
angularVelocity = Vector3.zero;
}
else
{
var rotationAngle = AngleAroundAxis (transform.forward, turnDir, Vector3.up);
angularVelocity = (Vector3.up * rotationAngle * turningSmoothing);
}
Here is how to calculate the angle around an axis (make it turn around its head y axis)
public static float AngleAroundAxis (Vector3 dirA, Vector3 dirB, Vector3 axis)
{
// Project A and B onto the plane orthogonal target axis
dirA = dirA - Vector3.Project (dirA, axis);
dirB = dirB - Vector3.Project (dirB, axis);
// Find (positive) angle between A and B
float angle = Vector3.Angle (dirA, dirB);
// Return angle multiplied with 1 or -1
return angle * (Vector3.Dot (axis, Vector3.Cross (dirA, dirB)) < 0 ? -1 : 1);
}
To make sure it doesn't overshoot make use of a in range calculation and the adjust the speed for the component above. You can do this with ( b - a).magnitude or Vector3.Distance. The trick is to calculate the gradual deceleration or you can just make it stop with a speed = 0. Let me know if you need more help.
GameObject Player;
public float speed;
public float rotationSpeed=3f;
public float howCloseToPlayer=3f;//Adjust here to stop enemy in a safe distance from Player to avoid moves in a circular orbit around the player
//set the position Y to be stable
public float minY = 0f, maxY = 0.01f;// keep position Y (avoid fall through)
public Vector3 currentPosition ;
void Start () {
Player=GameObject.Find("Player");
speed = Random.Range(3.0f,10.0f);
rotationSpeed=3f;
}
void Update () {
// get the position to a variable
currentPosition = transform.position;
// modify the variable to keep y within minY to maxY
currentPosition.y = Mathf.Clamp( currentPosition.y, minY, maxY);
// and now set the transform position to our modified vector
transform.position = currentPosition;
//rotate to look at the player
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(Player.transform.position - transform.position), rotationSpeed*Time.deltaTime);
//move towards the player
transform.position += transform.forward * speed * Time.deltaTime;
if (Vector3.Distance(Player.transform.position,transform.position)<=howCloseToPlayer){
rotationSpeed=0;
speed=0;
//Do Damage or whatever you want in here
}
}
// add outside functions
var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 10;
var MinDist = 5;
// add to Update
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist){
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
//Here Call any function U want Like Shoot at here or something
}
}