Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by AssignMeaning, Aug 31, 2014.

  1. AssignMeaning

    AssignMeaning

    Joined:
    Aug 31, 2014
    Posts:
    4
    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.
     
    Last edited: Aug 31, 2014
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    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.
     
    AssignMeaning likes this.
  3. AssignMeaning

    AssignMeaning

    Joined:
    Aug 31, 2014
    Posts:
    4
    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.
     
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    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.
     
    Last edited: Aug 31, 2014
    AssignMeaning likes this.
  5. AssignMeaning

    AssignMeaning

    Joined:
    Aug 31, 2014
    Posts:
    4
    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.
     
  6. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    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.
     
  7. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Just to be sure... have you checked the floor orientation is perfectly horizontal?
     
  8. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    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.
     
  9. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    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. :)
     
    Polymorphik likes this.
  10. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Physics minor :)
     
  11. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    You need some drag perhaps