How to spawn the player object in a platformer game in Unity

  • Detect player’s fall.
  • Rearrange the player’s transform position to a predetermined location.
  • Reduce life count from total lives. (Restart the game in case ran out of lives.)
  1. Detection of the fall.
  • Create a simple 3D cube object and stretch it on X axis.
  • Disable the mesh renderer and enable the “IsTrigger” on this object.
  • Create a new script on this object and add a trigger check method.
Unity
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
//DO SOMETHING.
}
}
  • Add a private variable to keep a counter of total lives.
  • Add a public method to reduce the count of lives upon a call.
private int _lives = 3;public void reduceLives()
{
_lives--;

if (_lives < 1)
{
DO SOMETHING.
}

}
  • Create a text object on the canvas to display player lives.
  • Get a reference to this text object via a serialized field on this associated UIManager script.
  • Add a method that displays the latest lives count on the screen.
[SerializeField]
private Text _livesText;
public void updateLivesText(int lives)
{
_livesText.text = "Lives " + lives.ToString();
}
  • Create an empty game object and copy the transform component values from the player object to this object’s transform component.
  • Open the fall detection script and get a reference to this object over there by adding a serialized field.
Unity
[SerializeField]
private GameObject _positionObject;
Unity
  • In the trigger method, use this object to rearrange the player’s position upon falling down on the dead end object.
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{

other.transform.position = _positionObject.transform.position;
}
}
  • In the trigger method above, get a reference to the player script component of the player object and call the reduce lives method.
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
Player player = other.GetComponent<Player>();

if(player != null)
{
player.reduceLives();
}

other.transform.position = _positionObject.transform.position;
}
}
  • In the player script, make a call to the UI method to display latest lives count.
public void reduceLives()
{
_lives--;
_UIManager.updateLivesText(_lives);

if (_lives < 1)
{
DO SOMETHING.
}
  • In case of running out of all lives, restart the game using scene management library.
using UnityEngine.SceneManagement;public void reduceLives()
{
_lives--;
_UIManager.updateLivesText(_lives);

if (_lives < 1)
{
SceneManager.LoadScene(0);
}


}
  • In the trigger method of the detection object, get a reference to the character controller.
  • Once null checked, disabled it for a minute time frame by using a coroutine. Once the time passes, reenable it in the coroutine itself.
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
Player player = other.GetComponent<Player>();

if(player != null)
{
player.reduceLives();
}

CharacterController cc = other.GetComponent<CharacterController>();

if (cc != null)
{
cc.enabled = false;
StartCoroutine(characterControllerEnabled(cc));

}

other.transform.position = _positionObject.transform.position;
}
}
IEnumerator characterControllerEnabled(CharacterController controller)
{
yield return new WaitForSeconds(0.5f);
controller.enabled = true;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store