How to find the bottom-centre of a sprite?

I have a player ship sprite that is facing upwards and a thruster particle that is facing downwards. Basically, I’m trying to figure out how to make the thruster’s position to be placed in the bottom-centre of the ship but I have no idea how. Also, how do I make the thruster rotate according to the rotation of the player so that it always faces the correct way whenever the player rotates? I have a screenshot below if you need visualisation on what I’m trying to achieve and the code I’m working on.

using UnityEngine;
using System.Collections;

public class ThrusterController : MonoBehaviour
{
  public SpriteRenderer player;

  private bool isThrust = false;

  void Update()
  {
    var em = gameObject.GetComponent<ParticleSystem>().emission;

    // Thruster on/off
    if (Input.GetKey(KeyCode.Space) && isThrust == false)
    {
      isThrust = true;
      em.enabled = true;
    }
    else
    {
      isThrust = false;
      em.enabled = false;
    }
  }
}

Screenshot:
2748345--198174--Capture.PNG

You can place the particle system as a child of the player (then it follows and rotates with player),
and in particle system set Simulation Space: World (so that the particles wont follow player movement)

2748368--198180--upload_2016-8-11_17-36-48.png

Thanks that worked!
And for others who is reading this, can someone post an alternative solution by scripting? I remembered figuring this out long ago with sprite.bounds but I forgot how to do it again :confused:

float centerX = player.bounds.size.x / 2 // center of the sprite in the -> direction

As for the Y direction, you either use the player.bounds.size.y or 0, not sure what Unity uses as the 0.0 position, bottom or top left.

This also assumes you use the power of 2 for the sprites ( 2 - 4 - 8 - 16 - 32 - 64 - etc. )

1 Like