x


2D - Locking The Z Axis

So, I'm making a 2D Platformer. I've been able to add some nifty features, but lately I've run into a problem. I would like my character to only move on the X and Y axis without any movement in the Z axis. Everytime I collide with a box, my character falls off the platform on the Z axis which I don't want. I've heard about configurable joints, but I'm still unclear about this all.

Thanks In Advance

more ▼

asked Jun 26 '10 at 01:46 PM

Flipbee9 gravatar image

Flipbee9
73 19 20 25

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

This question has been answered in multiple places before. There are 2 common solutions. One, in your player code, every frame call:

//JS
function Update () {
    transform.position.z = 0;
}

//C#
//using UnityEngine... public class...
void Update () {
     Vector3 pos = transform.position;
     pos.z = 0;
     transform.position = pos;
}

or you can add a configurable joint. Then go to z motion and choose locked instead of free or limited.

Other links.

Locking desired axis

more ▼

answered Jun 26 '10 at 01:55 PM

Peter G gravatar image

Peter G
15.1k 16 44 137

Thanks for the quick response :D

Jun 26 '10 at 01:57 PM Flipbee9

@Peter G, one minor point - that's Javascript. In C# you can't assign to Z, you have to do: transform.position = new Vector3(x, y, 0)

Jun 27 '10 at 06:26 PM Cyclops

Right, I'll post the C# version. Most newcomers seem to choose JS because all the documentation is JS so I write it as such.

Jun 27 '10 at 08:06 PM Peter G

@Peter G, true, I wasn't suggesting you change the post, I figured anyone seeing the post would also see my comment, which would be enough notice. :)

Jun 27 '10 at 10:38 PM Cyclops

The C# script completely froze my character so that it wouldn't move in any direction. Anyone know why? I created a C# script with only the contents above and attached it to my gameObject (character controller).

Mar 26 '12 at 10:36 PM amaranth
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1071
x129
x27

asked: Jun 26 '10 at 01:46 PM

Seen: 4987 times

Last Updated: Mar 26 '12 at 10:36 PM