- Home /
Restrict 2D object to path/spline
So I'm working on a 2.5D game that has loops. My entire issue here is no matter what, my character's Z position is always 0. I've tried using triggers that set the character's Z position with no luck. The red path is the one my character is currently taking, locked on the Z axis, and the blue path is the one I would like my character to take.
So my main question is, what is a good spline asset and how can I make my character follow it?
Answer by IsaiahKelly · Sep 09, 2017 at 03:50 AM
Something is locking your character's Z position, but it's impossible for anyone here to tell you why. At least not without more information. However, you will probably need to debug and figure this out yourself, since if you knew exactly what was causing it you probably wouldn't need any of us to help you anyway. :)
All I can really suggest is to try to isolate the issue as much as possible. This will help you narrow down the possible causes. Maybe an animator controller or root motion is locking the position?
A very bad "hack" until you figure out what's causing it or if you're just lazy would be to just forcibly reset the Z position in late or fixed update to whatever you want like this:
// whatever you want the Z position to be.
public float zOffset = 10f;
// cache the transform to improve performance.
private Transform m_Transform;
private void Awake()
{
m_Transform = GetComponent<Transform>();
}
void FixedUpdate()
{
m_Transform.position = new Vector3(m_Transform.position.x, m_Transform.position.y, zOffset);
}
It's not using root motion, but I could check the movement scripts to see if that's the issue.
I can't find anywhere in scripts where the Z axis is locked; I've tried moving the character along Z in play mode which actually works until I jump. If it helps, the engine I'm working with is called Sonic Realms and it's built only for 2D which is probably why I can't seem to modify the Z axis during runtime.
Then the jump method is definitely resetting the Z position and probably some other function is doing so at startup. You need to remember that every bit of code that touches the transform position will probably also be resetting the z axis/position. So this might be hard to change or track down.
However, I've added a simple hack to the answer above that might help you for now.
If you're using physics to move you should also check and see if you have the rigidbody's Z axis frozen under constraints. If you're using 2D physics than you would need to either switch to the 3D physics system or just stop setting the position through physics.
Your answer
Follow this Question
Related Questions
How to go about constraining a player to a path? 0 Answers
object follow a spline path 1 Answer
Follow curved path with dynamic speed 2 Answers
Add force along a path 1 Answer
PutOnPath iTween spline but not restrict in the up direction (jump) 3 Answers