- Home /
move enemy to old position of player
hello iam new in unity how i can move enemy to old position of player and back to start point .
I don't want the enemy follow the player. just go to the old player's position and when reach back to start enemy position. And a time for this movement, for example, every minute. like picture
Answer by rhapen · Oct 26, 2020 at 09:49 PM
I dont know what is your code but simply sending the vector position to the enemy from player when the enemy is at staring position and player in the position enemy should go to would be good solution as any. Send message from player script would be the way i would go https://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html
you dont really give much where to start. We do not know how do you currently move the characters. Here is one example how to do it if you use RigidBody
public class EnemyExampleClass : MonoBehaviour
{
public Vector3 playerExPosition; //you might want to also assign this from outside if you want it happen more than once
Vector3 enemyExPosition;
Rigidbody rb;
public bool goingToPlayerExPosition = true; //made public sou you can make it true by outside script, trigger, whatever
void Start()
{
rb = GetComponent<Rigidbody>();
enemyExPosition = this.transform.position;
playerExPosition = GameObject.FindWithTag("Player").transform.position; //find player by tag.. you have to add tag to player to use it this way
}
void FixedUpdate()
{
if(goingToPlayerExPosition) target = playerExPosition
else target = enemyExPosition;
if(this.transform.position == playerExPosition) goingToPlayerExPosition = false
rb.MovePosition(target + transform.forward * Time.deltaTime);
}
}
I don't know what the correct code is. I tried a lot of codecs The problem the enemy always follows the player and not return to the start position
Answer by taiet · Oct 27, 2020 at 08:01 PM
help me please
@taiet updated my answer to give you little more information on how to. But your question is really too broad and you need to give the examples of code you are using to achive what you are currently doing. there are many ways to move objects, characters, player.