Home > Blockchain >  How get top center and bottom center position point from SpriteRenderer Unity
How get top center and bottom center position point from SpriteRenderer Unity

Time:11-03

I want to get from the SpriteRenderer these positions from the screenshot, this is the top center point and the bottom center point to create objects at these positions, so I need the global position of these points.

enter image description here

I already tried to use Bounds, but they did not give me the result I needed, they show the wrong center, min and max positions, or rather they were created in other places. Maybe I misunderstood something? The search logic is written in a different object, and the SpriteRenderer is on a different object,

        Bounds spriteBounds = SpriteRenderer.sprite.bounds;

        _centerPointValue = spriteBounds.center;
        _rightUpPointValue = spriteBounds.max;
        _leftBottomPointValue = spriteBounds.min;

CodePudding user response:

Having the correct

  • Center
  • BottomLeft
  • TopRight

calculating your desired

  • BottomCenter
  • TopCenter

should be trivial:

var bottomCenter = _rightUpPointValue;
bottomCenter.x = _centerPointValue.x;

var topCenter = _rightUpPointValue;
topCenter.x = _centerPointValue.x;

tl;dr: Instead of SpriteRenderer.sprite.bounds use SpriteRenderer.bounds


Actually using those works just fine for me IF

  • The renderer isn't anyhow scaled
  • The renderer isn't anyhow moved
  • The renderer is using Draw Mode = simple

In this case you could use the Sprite.bounds (shouldn't though - see below)

public class Example : MonoBehaviour
{
    private class NamedPosition
    {
        public NamedPosition(Vector2 position, string label)
        {
            Position = position;
            Label = label;
        }

        public Vector2 Position { get; }
        public string Label { get; }
    }

    public GameObject prefab;

    private void Start()
    {
        var spriteRender = GetComponent<SpriteRenderer>();
        var bounds = spriteRender.sprite.bounds;

        var positions = new List<NamedPosition>();

        var center = bounds.center;
        positions.Add(new NamedPosition(center, nameof(center)));
        
        var topRight = bounds.max;
        positions.Add(new NamedPosition(topRight, nameof(topRight)));
        
        var bottomLeft = bounds.min;
        positions.Add(new NamedPosition(bottomLeft, nameof(bottomLeft)));
        
        var topCenter = topRight;
        topCenter.x = center.x;
        positions.Add(new NamedPosition(topCenter, nameof(topCenter)));
        
        var bottomCenter = bottomLeft;
        bottomCenter.x = center.x;
        positions.Add(new NamedPosition(bottomCenter, nameof(bottomCenter)));

        foreach (var namedPosition in positions)
        {
            var instance = Instantiate(prefab, namedPosition.Position, quaternion.identity);
            instance.name = namedPosition.Label;
        }
    }
}

enter image description here


If this is not the case (probably in the most use cases) you rather want to use the enter image description here

CodePudding user response:

I have imported the sprite into unity, and added it to a SpriteRenderer:

imported image

I have also created 2 markers, topMarker and bottomMarker, those are a couple of gameObjects and I’m pretending to take their Transforms and position’em on the selected bounds (center-top and center-bottom of the sprite rendered).

As we can see, there are 3 relevant transforms: 1 for SpriteRenderer, and 2 for our markers. So we can get the center of the sprite renderer, if we get the component and then access to it’s transform:

SpriteRenderer sprite;

sprite = GetComponent<SpriteRenderer>();

Debug.Log("POSITION: "   sprite.transform.position);

Once we got this, we can get the max bounds from the sprite renderer:

Debug.Log("Center Top: "   (sprite.transform.position.y   sprite.bounds.max.y));

And now we can test it positioning our markers automatically:

topMarker.position = sprite.transform.position   new Vector3(0,sprite.bounds.max.y,0);
bottomMarker.position = sprite.transform.position   new Vector3(0,-sprite.bounds.max.y,0);

Then, if we hit play, we can see our markers positioning on said point:

markers moved

Here’s the code so you can try it and understand the logic behind this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class getBounds : MonoBehaviour
{
   
    SpriteRenderer sprite;
 
    [SerializeField]
    Transform topMarker, bottomMarker;
 
    void Start()
    {
        sprite = GetComponent<SpriteRenderer>();
       
        Debug.Log("INFO:");
        Debug.Log("-------------------------");
        Debug.Log("POSITION: "   sprite.transform.position);
        Debug.Log("BOUNDS: "   sprite.bounds);
        Debug.Log("Center Top: "   (sprite.transform.position.y   sprite.bounds.max.y));
 
        topMarker.position = sprite.transform.position   new Vector3(0,sprite.bounds.max.y,0);
        bottomMarker.position = sprite.transform.position   new Vector3(0,-sprite.bounds.max.y,0);
    }
}
  • Related