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

rigid body moves faster diagonally even when normalized?

Discussion in 'Physics' started by zeropointblack, Jan 18, 2021.

  1. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    197
    hello.

    i have 3d rigid body player using add force. even when normalized it moves faster diagonally (analog stick). how to fix.

    thanks,

    p.s. i can see the values coming out of normalize are like .4 and .9 diagonally together. is that normalized? seems that would be higher than 1 to me.
     
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    There is not a problem with normalized, you have a logic error in your code. Add in some Debug.Log() statements and see what is happening.

    If you want more help, you need to write a small replicator code and post it here explaining what you expected to happen and what is happening. I would suggest you try to make your replicator use a keyboard for input as people without controllers probably won't bother looking otherwise. You should be able to post something trivial that moves a cube around on a plane to illustrate the diagonal issue.

    Regarding your p.s.:
    normalized means the magnitude (length) of the vector is equal to 1. In a 2D sense this is the length of the hypotenuse of a right angled triangle. For example if you had x = 0.40 and z = 0.92 your magnitude would be 1

    Sqrt(0.40^2 + 0.92^2) = 1
     
    Last edited: Jan 19, 2021
  3. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    197

    whoa. sounds like its working properly then.

    well then what i dont get is why every tutorial says add normalize, or normalized, (whichever, cant remember), to your input vectors and then it wont run faster diagonally. and they work. but for me suddenly, they stopped working. are they not supposed to work? sounds like no after reading this, but im sure ive seen them work.

    also, they used to work for me when i used character controller instead of rigidbody. strange.

    ill check my code again.
     
  4. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    Firstly, I would reiterate my advice to make a simple replicator that just moves a cube around on a flat surface - you will probably figure out your issue in making it, and if you don't, posting the code will make it easier for the rest of us to assess the problem.

    Normalize is a very simple method to make sure a vector has a magnitude of 1, whatever the initial values were*. However, you need to consider two important things:

    1) If you are hoping to use non-binary inputs, e.g. analogue control sticks, then realise that normalizing will give you an output of magnitude 1, so pushing the stick half way will not let the player move at half speed.

    2) *Normalized vectors are always magnitude 1, (unless the initial values produce a magnitude < 1e-5, in which case Vector3.zero is returned). So you may get an output direction even when you are expecting (near) zero due to input decay or floating point precision.

    You may want to look at Vector3.ClampMagnitude instead, which you could use to allow a vector of magnitude 0 to 1.

    Edit:
    I just found this, which may interest you https://amorten.com/blog/2017/mapping-square-input-to-circle-in-unity/