- Home /
Move with Rigidbody2D in the Z axis
Hi guys.
Can you think of any way to move a sprite in the Z axis with rigidbody2D.AddForce? I can only move in the X axis and Y axis.
I know there is no Z direction in 2D physics, but I need work with X and Z.
Any idea?
Thanks.
Answer by JanusMirith · Oct 14, 2014 at 03:29 AM
Hmmm, That is a odd one, what you could do is have the simulation and graphics separate and sync the graphics up via script:
/// <summary>
/// The logic and physics
/// </summary>
public Rigidbody2D thePhysics;
/// <summary>
/// All the eye-candy
/// </summary>
public Transform theGraphics;
/// <summary>
/// Sync the two states, call this from update or coroutine
/// </summary>
public void syncSimulation()
{
//Sync them!
theGraphics.position = new Vector3(thePhysics.transform.position.x, 0f, thePhysics.transform.position.y);
//I would maybe have a little offset for the system so I could see them side by side.
}
Answer by Pyrian · Oct 14, 2014 at 02:22 AM
You cannot do that directly, but nothing prevents you from doing it indirectly.
If you're ignoring Y altogether, then you can just set Z to the value of Y in each update. This could cause problems with your camera, however.
Alternatively, you can do all the physics with invisible objects and map them to your sprite objects in FixedUpdate (or Update, if you're using interpolation). So, each Sprite object script has a reference to a corresponding physics object, and you just set the transform of the sprite to the X/0/Y of the invisible physics object.
Answer by Duende · Oct 14, 2014 at 01:55 PM
Thanks for the help.
In the end, I have done is create a cylinder, use Rigidbody.AddForce Forward and Back in the cylinder movement script and convert the sprite in to the child of the cylinder.