Search Unity

Y axis rotates too fast no matter what speed i set

Discussion in 'General Discussion' started by NewMagic-Studio, Sep 1, 2020.

Thread Status:
Not open for further replies.
  1. NewMagic-Studio

    NewMagic-Studio

    Joined:
    Feb 25, 2015
    Posts:
    454
    I have a gameobject and a child in the gameobject, the parent has a script with this code

    rotationYAxis += Input.GetAxis("Mouse Y") * Time.deltaTime * 10f;
    rotationYAxis = Mathf.Clamp(rotationYAxis,-1f,1f);
    rotationXAxis += Input.GetAxis("Mouse X") * Time.deltaTime * 10f;
    rotationXAxis = Mathf.Clamp(rotationXAxis,-1f,1f);
    transform.rotation = Quaternion.AngleAxis(rotationYAxis * 90f, transform.right);
    // transform.rotation *= Quaternion.AngleAxis(rotationXAxis * 90f, transform.up);

    It works fine, but if i want to rotate around the Y axis and remove the comment in last line it doesnt work. If i just try to rotate the Y axis it rotates around too fast, no matter what speed i set, why this happens??
     
  2. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    This really isn't the place for code talk. Should be in Scripting.

    Maybe I'm missing something, but the last commented-out line is using rotationXAxis, not rotationYAxis?

    Also it's been a while since I messed with Quaternions, but it looks like you're explicitly setting the rotation, rather than modifying the value.
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    Unless I'm mistaken, Mouse X/Y returns values in PIXELS mouse traversed since the last frame.

    And you multiply this value by delta time and 90 degrees. Meaning if the user sneezes at the mouse, it will rotate by 90 degrees. Per frame. Oh, and get this, your Y rotation seems to be accumulating over time (you're setting it to += and it doesn't seem to be a local variable). So if the user moves mouse a little, it'll be continuously spinning.

    Normally rotation using mouse is performed without taking dleta time into account. You multiply desired rotation speed by whatever distance mouse traversed.

    If you want inertia, you can store it as angular velocity and cap that.

    Additionally you're basically resetting transform every time in the line with Y rotation.

    -------

    Anyway. If you're trying to implement yaw/pitch/roll movement or shooter like movement with 3d position and 2d "heading", at least name the angles properly. Y rotation usually denotes rotation AROUND y axis, and in unity transform.right corresponds to x axis of the local coordinate system.
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    Wrong forum.
     
Thread Status:
Not open for further replies.