# Dancing Z value

**URL:** <https://discussions.unity.com/t/dancing-z-value/422240>\
**Category:** Physics\
**Created:** [September 4, 2010, 5:38am UTC](https://discussions.unity.com/t/dancing-z-value/422240 "2010-09-04T05:38:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arvzg1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/arvzg1/32/500725_2.png) [@arvzg1](https://discussions.unity.com/u/arvzg1)\
**Post date:** [September 4, 2010, 5:38am UTC](https://discussions.unity.com/t/dancing-z-value/422240/1 "2010-09-04T05:38:27Z")

</div>

hey all 🙂

I’m making a side scroller currently and I have just written part of my character controller script. I’ll quickly give a rundown on how it all currently works:

Left and right movement uses the classic GetAxis method: (In **FixedUpdate()**)

```auto
var translation = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
transform.Translate(translation, 0, 0);

```

and Jumping:

```auto
if (canJump)
{
  if (Input.GetKeyDown("space") || Input.GetKeyDown("w"))
    rigidbody.AddForce (Vector3.up * jumpForce);
}

```

So as you can see my character - currently just a capsule - has a rigidbody component. it also has a box collider. Freeze rotation is enabled.

Now I don’t want my character to be bouncy as it lands on the ground after jumping so I added a physic material with dynamic friction and static friction set to 1, and bouncyness set to 0.

The ground itself is a terrain which has the same physic material as the character.

**Now the problem:**

When I start the game, my character which I placed initially above the ground is affected by gravity and lands on the ground. When it lands on the ground its Z value which is initially 0 changes itself to a very small value like 0.00068. As I walk around in the level it gets larger no matter what direction I go. Eventually the value gets large enough and my character falls off the ground.

Why is this happening? How can I stop it? ☹

A curious observation: If I set dynamic and static friction to 0 the problem doesn’t happen. Z stays 0 the whole time, which is great, but since the friction is 0 my character would very slowly slide across the ground after moving and seem to never stop.

---

<div class="post-metadata">

**Author:** ![Dantus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dantus/32/148077_2.png) [@Dantus](https://discussions.unity.com/u/Dantus)\
**Post date:** [September 4, 2010, 10:11am UTC](https://discussions.unity.com/t/dancing-z-value/422240/2 "2010-09-04T10:11:33Z")

</div>

If you are using a rigidbody, you should not use transform.Translate, instead you should also use rigidbody.AddForce or rigidbody.MovePosition.  
Did you have a look at the Lerpz tutorial? I think in the tutorial a Configurable Joint is used, which restricts the movement along the z axis. But as it is still not 100 percent stable, they have a script that sets the z value in every FixedUpdate or Update (not sure which one) to zero.

---

<div class="post-metadata">

**Author:** ![arvzg1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/arvzg1/32/500725_2.png) [@arvzg1](https://discussions.unity.com/u/arvzg1)\
**Post date:** [September 4, 2010, 1:21pm UTC](https://discussions.unity.com/t/dancing-z-value/422240/3 "2010-09-04T13:21:49Z")

</div>

I had a quick look at the lerpz tutorial, it was quite long so I didnt go through it all. I looked at the configurable joint part just now and tried it however it didnt really work, I managed to get it to restrict rotations which pretty much does the same thing as Freeze Rotations, but locking the z axis didnt seem to work even after I changed the axis to the right one (should be y)

I just changed the transform.Translate to Rigidbody.MovePosition but the same problem still happens ☹

---

<div class="post-metadata">

**Author:** ![Dantus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dantus/32/148077_2.png) [@Dantus](https://discussions.unity.com/u/Dantus)\
**Post date:** [September 5, 2010, 8:24am UTC](https://discussions.unity.com/t/dancing-z-value/422240/4 "2010-09-05T08:24:04Z")

</div>

What about a script that sets z to zero every frame?

---

<div class="post-metadata">

**Author:** ![andeeeee](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@andeeeee](https://discussions.unity.com/u/andeeeee)\
**Post date:** [September 7, 2010, 3:34pm UTC](https://discussions.unity.com/t/dancing-z-value/422240/5 "2010-09-07T15:34:25Z")

</div>

The configurable joint isn’t too hard to erm… configure. You just need to set the Z motion property to Locked and enable the Configured in World Space option. This will keep the character locked at the Z coordinate it started off with.
