Unexpected movement on Z axis while using input.GetAxis("Horizontal")

Hello.
Here is my problem. I am following a tutorial from youTube:

Everything worked fine, just as the demonstrator showed. Except he ran into a problem he couldn’t fix himself. I can’t figure it out either. It’s baffling.

Here’s what I did. I wrote the following script and dragged it into the player object, which is a cube with a rigidbody and box collider. There is also a floor, which the player sits on.

When pressing left and right, I get movement along the X axis as expected, but also a little movement in the Z axis. (The same happens for the up/down movement intended for the Z axis producing little amounts of movement in the X axis.)

using UnityEngine;
using System.Collections;

public class PlayerMovement: MonoBehaviour {
public float moveSpeed;
public float maxSpeed = 5f;

private Vector3 input;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
input = new Vector3(Input.GetAxis(“Horizontal”), 0, Input.GetAxis (“Vertical”));

if(rigidbody.velocity.magnitude < maxSpeed)
{
rigidbody.AddForce(input * moveSpeed);

}

}
}

EDIT: When I turn Gravity Off, the problem goes away. No extra Z movement will be introduced to the player. But without gravity, the player flies in one direction.

You are adding a force to the cube. If you want to avoid getting certain axis pushed on, you have to specify what values to add on the force. rigidbody.AddForce(float x, float, y, float, z) might be what you want. You will need to modify your code and clean it up. After that it should work. If you run into any problems. Just comment here or send me a ping.

Hope this helps.

1 Like

Thanks for the suggestion. I updated the question with an additional edit. It is about how the input and movement on the cube works perfectly fine when gravity is turned off. This means the forces are being added to the cube properly, I believe. Since the problem is occurring when gravity is on and while colliding with the floor, I’m assuming there will be something wrong with the floor or the interaction between the cube and the floor.

I made the project that was in the video. The answer is simple. Its physics. The issue here is the friction. In real life if you push something from side to side on a frictionless plane, if the force F applied to the object is perfect with no angular modifications, it will move from side to side in a perfect manner. Friction however will move the object slightly randomly depending on the surface. If you want to reduce the amount of friction you must reduce the grounds friction.

Edit: I shouldn’t say random
Dry Friction is f = μ(Friction Constant) * N (Normal Force)
ƒ = μN

What you need to do is reduce μ.
Inside unity go to
1.) Projects → Create → Physics Material. Name it.
2.) Set Dynamic and Static Friction to 0.
3.) Set Friction Combine to ‘Minimum’
4.) Drag and drop the Physics Material to the collider of the ground.

Let me know if this helps.

1 Like

Hello Polymorphik,
thank you for your reply. I have tried the solution and the effect is the same as turning off gravity. Due to the absence of friction, it is impossible to control the player cube. I tried different friction values, but the problem remains.

I should note that the Z axis deviates more when the right button is pressed. This holds true at different levels of friction, with and without the physics material.

If the friction is causing you problems, I suggest you switch to moving the actual transform of the player rather than the applying a force. If you do this, ensure that you freeze the X and Z position in the Rigidbody. This will be the only way to get the result you want without having to mess with friction. Also yes, a frictionless surface is difficult to control, you can reduce the speed of the player or mess with the controls but, I suggest you just change the transform.position.

Hope this helps.

Just to be sure… have you checked the floor orientation is perfectly horizontal?

Friction will always be present on an object as the value is cosine of the angle between the two objects. Only at (n*π - π/2) does friction not occur this takes place when the object in motion is perpendicular, meaning it is getting lifted off the ground or is falling off. So the angle of the surface object here has no meaning, as dynamic friction always exists as long as the two bodies are touching one an other V is not 0. The fact of the matter is, he is applying a force P.

Fχ = μNcosθ, N = mg
Fχ = μmgcosθ
ΣFχ = Pχ + μmgcosθ, where θ = π
ΣFχ= Ρχ - μmg

So you can see, μ always has an effect on the sum of the forces ΣF. This is just on the X axis, the same would hold true for the rest. So the Z-Axis he is seeing is the friction fighting against the slippage. This causes the cube to “bump” therefore moving without uniformity. Set μ → 0 and the P will be the only force. When he did what I told him to use the physics material and set μ = 0 it worked. Only problem is, it’s difficult to control something that is slipping.

Mmmmmh… It seems a bit more complicated than I expected. I only understand “cube” and perhaps “Z-Axis” of all you said, but I trust you. :slight_smile:

1 Like

Physics minor :slight_smile:

You need some drag perhaps