Getting player last seen position?
Hi,
I am trying to charge my enemies to player when they are closer than 5f, with my code it charges but while it's charging it also follows the player, I am trying to charge it straight to, not follow the player when it's charging. How can I solve this?
My EnemyFollow code;
public class EnemyFollow : MonoBehaviour
{
public float speed;
public float chargingSpeed;
private Transform target;
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
void Update()
{
if (Vector2.Distance(transform.position, target.position) > 0.5f)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
if (Vector2.Distance(transform.position, target.position) < 5f)
{
Charge();
}
else
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
}
if (transform.position.x < target.position.x)
{
transform.localRotation = Quaternion.Euler(0, 180, 0);
}
else
transform.localRotation = Quaternion.Euler(0, 0, 0);
}
void Charge()
{
transform.position = Vector2.MoveTowards(transform.position, target.position, chargingSpeed * Time.deltaTime);
}
}
Answer by IINovaII · Nov 02, 2019 at 08:22 PM
The transform component of the player keeps updating the position whenever the player moves. Hence why the enemy follows the player during the charging. In order to avoid this, save the position of the player in a Vector2 variable when the player is within the distance and update this variable only when the charge is complete.
Something like this would work (Haven't tested the code):
public class EnemyFollow : MonoBehaviour
{
public float speed;
public float chargingSpeed;
private Transform target;
private bool isCharging;
private Vector2 positionToChargeTowards;
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
void Update()
{
if (!isCharging)
{
if (Vector2.Distance(transform.position, target.position) > 5)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed *
Time.deltaTime);
}
else if (Vector2.Distance(transform.position, target.position) <= 5)
{
positionToChargeTowards = target.position;
isCharging = true;
}
}
else
{
if (transform.position.x < target.position.x)
{
transform.localRotation = Quaternion.Euler(0, 180, 0);
}
else
transform.localRotation = Quaternion.Euler(0, 0, 0);
Charge();
if (Vector3.Distance(transform.position, positionToChargeTowards) == 0)
isCharging = false;
}
}
void Charge()
{
transform.position = Vector2.MoveTowards(transform.position, positionToChargeTowards,
chargingSpeed * Time.deltaTime);
}
}
Give this a try and let me know if it matches the behaviour that you were looking for.
Answer by yasacan · Nov 03, 2019 at 12:59 PM
Thank you for the answer @IINovaII , but this didn't work. Your solution is different than what I am trying to do, my explanation might not that clear sorry about that, let me try again;
On my code Enemies start to follow player, when Enemies are closer than 5f then they charge, but Enemies are still following the player while charging on my code. I am trying to make them charge straight to the player's position when they got closer than 5f.
Also forgot to mention by charging, I meant it speeds up to player's position for like 2 seconds like an arrow then it goes back to normal speed and keeps following the player.
I need to charge Enemy to Player's position when they entered this section I guess
if (Vector2.Distance(transform.position, target.position) < 5f)
{
Charge();
}
Enemy shouldn't be following while charging, it should directly to speed up to that direction for 2 or 3 seconds. But I couldn't figure it out because it gets player position on every frame because it on update, I think right way is using boolean but couldn't figured it out how to.
I probably should've asked this earlier but, is this for a 2d game? Is there a fixed distance for the enemies to charge? By that I meant, the enemies can charge past the player location until the fixed distance is reached.
I made a little change to the code that I posted. The $$anonymous$$imum distance between player and enemy to charge was too small to notice anything.
if (!isCharging)
{
//Changed 0.5f to 5
if (Vector2.Distance(transform.position, target.position) > 5)
{
transform.position = Vector2.$$anonymous$$oveTowards(transform.position, target.position, speed * Time.deltaTime);
}
//Changed 0.5f to 5
else if (Vector2.Distance(transform.position, target.position) <= 5)
{
positionToChargeTowards = target.position;
isCharging = true;
}
}
That was my bad, yes it is a 2D game. This is what I was looking for thank you! But there are some problems, after the charge enemies stop following the player, they should keep follow the player on normal speed after it charges, should I add a cooldown on somewhere after it charges?
Your answer
Follow this Question
Related Questions
Make object follow another object while keeping the same distance from it 1 Answer
How to change audio based on distance from multiple prefabs? 0 Answers
Why doesn't my object move when I use transform.position? 0 Answers
My script to move a gameobject to the closest tagged gameobject doesn't work 1 Answer
Jittering transform.position values when GameObject is not moving. 1 Answer