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

The exercise here demonstrates a simple approach to deploy spawning of the player object in a platformer game.

Below are the basic tasks to accomplish this.

  • 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.
}
}

So far, the detection is settled.

Next part is to make a few calls to the player object as well as the UI canvas.

PLAYER OBJECT

  • 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.
}

}

UI OBJECT

  • 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();
}

2. Rearrange the player position to a predetermined location.

  • 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;
}
}

3. Reduce life count from total lives.

  • 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);
}


}

So far the basics tasks have been addressed successfully.

However, there is a small glitch that needs attention.

As soon as the detection would occur, the transform would be rearranged so quickly that the user would end up seeing player falling. To address this glitch, the character controlled should be regulated while the position rearrangement takes place.

  • 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;
}

That should be it :)

Here is the final result.

Thank you very much

--

--

--

Work in Progress.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Flutter Scientific Calculator Tutorial within 5 minutes for flutter beginners

C++ Operators: Learn C++ Operators with examples

Top 100 Hadoop Interview Questions

Monolith vs Microservices vs Serverless and what to choose for your business needs

ABN AMRO’s Streaming Architecture

Predictions for 2021

Create GitHub actions for SPFx solution

Lifting the Sail: How Spinnaker maps resources to Kubernetes

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
Samarth Dhroov

Samarth Dhroov

Work in Progress.

More from Medium

2.5D Platformer: Dev Progress Update

Cinemachine: How to create Camera Shake on Unity Cameras

Getting that Hand-Held Look on Cameras + Two Cinematic Challenges

Camera Look System in Unity!