- Home /
Created object not in 0,0 coordinate
I create object
GameObject carBody = new GameObject("CarBody");
carBody.tag = "Part";
SpriteRenderer renderer = carBody.AddComponent<SpriteRenderer>();
Sprite ChsBodySpr = Sprite.Create(cars[id] as Texture2D, new Rect(0, 0, cars[id].width, cars[id].height), new Vector2(0, 0));
ChsBodySpr.name = "sprite";
renderer.sprite = ChsBodySpr;
BoxCollider2D bodyBX = carBody.AddComponent<BoxCollider2D>();
bodyBX.size = renderer.size;
but his 0,0 is not in center of object. What can i do?
what i have
what i want
The second picture does show it in center, so... what's the problem? But if an object doesn't appear in the center while the position is 0,0 then you probably don't use the main object you want to place it in as a parent. So, if you are using a canvas, make sure you ass your sprite to the canvas, if you then use 0,0 as position it will be in the center of the canvas.
I fixed it by Sprite ChsBodySpr = Sprite.Create(cars[id] as Texture2D, new Rect(0, 0, cars[id].width, cars[id].height), new Vector2(0.5f, 0.5f));
Answer by Summit_Peak · Jun 04, 2018 at 01:56 PM
Try putting the Car Body gameObject as a child of another gameObject, then change the offset of the Car Body gameObject:

Your answer
Follow this Question
Related Questions
Left Click While Holding Right Click 1 Answer
How to get pivot of sprite in script 2 Answers
Turn the sprite 1 Answer
Help me understand SyncVars please 0 Answers
Change image on keypress 3 Answers