- Home /
Having a Sprite's position be different to it's prefab's
Hello. I've been using Unity for about 2 years now, and have a difficult question to ask.
I am making a 2D game, and have a rigidbody object. This object has a SpriteRenderer attached to it, so naturally, when the object moves, it's sprite moves with it.
What I want to do however, is have the object move normally (smoothly), but have the sprite move jarringly, giving off the illusion that the object is moving jarringly.
For example, when the object moves from x = 0.0 to x = 10.0, it goes 0.1f, 0.2f, 0.3f etc. But the way I want the sprite to move is for it's position to be the Mathf.Floor() of x, so: 1, 2, 3, 4 etc.
I've searched many topics on this, but there doesn't seem to be a way to move a SpriteRenderer independent of its GameObject. I would make the GameObject itself move jarringly, but that would interfere with it's rigidbody behavior.
This issue's been bugging me for days now. I really appreciate any and all help on this. Thanks in advance.
Answer by Jeff-Kesselman · Jun 26, 2014 at 03:48 PM
A SpriteRenderer has no position.
The only things that have position in the world are game objects.
My suggestion is that you make a child object to hold the sprite and adjust its position as you want.
In the editor, create an empty game object, then drag your sprite object over it in the hirearchy window.
or in code do something like this:
GameObject parentObj = new GameObject();
mySprite.transform.parent = parentObj.transform;
Hm... could I get an example of that please? Specifically, how I would assign it in the inspector.
Do I just make the object instantiate a blank GameObject at runtime and assign a sprite to it?
Thankyou so much! Unity makes it a little awkward, but it solved my problem.