Basically I have these scripts:
1. Player Movement:
public class Playermovement : MonoBehaviour
{
public float speed;
public float rotationSpeed;
void Update()
{
speed = GameObject.Find("GameManager").GetComponent<GameManager>().speed;
rotationSpeed = GameObject.Find("GameManager").GetComponent<GameManager>().rotationSpeed;
float translation = +speed;
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
}
}
2. Player Collision:
public class PlayerCollision : MonoBehaviour
{
void OnCollisionEnter(Collision playercollision)
{
if (playercollision.collider.tag == "Obstacle")
{
Debug.Log("Object Hit!");
FindObjectOfType<GameManager>().EndGame();
}
}
}
3. Game Manager:
public class GameManager : MonoBehaviour
{
public float speed = 7f;
public float rotationSpeed = 150.0f;
bool gameHasEnded = false;
public float restartdelay = 2f;
public void EndGame()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
speed = 0f;
rotationSpeed = 0f;
Invoke("Restart", restartdelay);
}
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
speed = 7f;
rotationSpeed = 150.0f;
}
}
So, I have the player dodging randomly spawned obstacles, with an object behind to destroy obstacles that have gone past (I’ll call it the “Destroyer”). I had this Destroyer parented under the player so that it would follow. I did this because It is around a spherical object and can’t work out any other way to do it. This worked fine until I made it so that the game ends when the player hits an object. So now because the Destroyer is parented under the player, whenever Destroyer hits an object so it can then destroy it, the game ends. How can I get around this? Thanks for trying to help!