First of all I want to apologize for my english, I’m french and I still do some mistakes.
What i want to do is to be able to tp all the enemies in my scene to their initial position every time that the player dies. The problem that I have is that when my enemy is turn to the right transform.localScale = new Vector3 (-1f, 1f, 1f);
and he kills the player he is immediately tp to the same position than the player and not to his original position.
In my scene I have several enemies with a script attach to them :
using UnityEngine;
using System.Collections;
public class EnemyMove : MonoBehaviour {
public float moveSpeed;
public float moveVelocity;
public Vector3 originPosition;
public GameObject particles;
public PlayerScript player;
public bool movingRight;
public bool tp;
void Start(){
originPosition = transform.position;
moveVelocity = moveSpeed;
player = FindObjectOfType<PlayerScript> ();
}
void FixedUpdate(){
if (tp) {
Instantiate (particles, transform.position, transform.rotation);
transform.position = originPosition;
Instantiate (particles, transform.position, transform.rotation);
tp = false;
}
}
void Update () {
if (transform.position.x - player.transform.position.x > 7f)
movingRight = false;
else if (transform.position.x - player.transform.position.x < -7f)
movingRight = true;
if (!movingRight) {
transform.localScale = new Vector3 (1f, 1f, 1f);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-moveSpeed, GetComponent<Rigidbody2D> ().velocity.y);
} else {
transform.localScale = new Vector3 (-1f, 1f, 1f);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveSpeed, GetComponent<Rigidbody2D> ().velocity.y);
}
}
}
In addition to this I have an other script in charge of managing the scene :
using UnityEngine;
using System.Collections;
public class LevelManagerArmed : MonoBehaviour {
public GameObject currentCheckpoint;
public CameraControllerArmed myCamera;
public PlayerScript player;
public float respawnDelay;
public static bool rendererActivate;
public Animator anim;
private EnemyMove enemyMove;
void Start () {
rendererActivate = true;
enemyMove = FindObjectOfType<EnemyMove> ();
myCamera = FindObjectOfType<CameraControllerArmed> ();
player = FindObjectOfType<PlayerScript> ();
anim = player.GetComponent<Animator> ();
}
void Update (){
anim.SetBool ("Awakening", false);
}
public void RespawnPlayer (){
StartCoroutine ("RespawnPlayerCo");
}
void FixedUpdate(){
if (!rendererActivate) {
player.GetComponent<Rigidbody2D> ().MovePosition (currentCheckpoint.transform.position);
}
}
public IEnumerator RespawnPlayerCo(){
EnemyMove[] Enemies = FindObjectsOfType(typeof(EnemyMove)) as EnemyMove[];
foreach (EnemyMove enemy in Enemies) {
enemy.tp = true;
}
myCamera.isFollowing = false;
player.enabled = false;
player.GetComponent<Rigidbody2D> ().velocity = Vector2.zero;
player.GetComponent<Collider2D> ().enabled = false;
rendererActivate = false;
player.transform.localScale = new Vector3 (1f, 1f, 1f);
yield return new WaitForSeconds (respawnDelay);
player.GetComponent<Collider2D> ().enabled = true;
anim.SetBool ("Awakening", true);
rendererActivate = true;
player.enabled = true;
myCamera.isFollowing = true;
}
}