Search Unity

Object revolving faster when moving towards zero degrees angle?

Discussion in 'Scripting' started by kr4ft3r, Jul 26, 2017.

  1. kr4ft3r

    kr4ft3r

    Joined:
    Feb 18, 2014
    Posts:
    10
    Hello.
    I am working on a game where player controlled object is moving along circular path, with fixed radius and fixed center position, the object's front always looking towards positive Z. So it is basically an object which can only move along the inner edge of an imaginary cylinder of a fixed radius, who's main axis is Z. Holding the left key reduces the euler angle relative to start angle, holding the right key increases it.

    The main part of achieving this circular motion is in these two lines:

    Code (JavaScript):
    1.  
    2. rotation.eulerAngles = Vector3(0, 0, currentRotation);
    3. transform.position = rotation * radius;
    4.  
    5. // currentRotation is angle relative to starting position, and is increased or decreased by horizontal input each frame
    6. // radius is fixed value Vector3(4.0,0,0), holds radius of imaginary cylinder
    7. // the transform.position = rotation * radius; is a formula I found online and it is doing the job, but I guess may be responsible for the problem I have
    The problem I have is the object's speed of revolving left or right is uneven, it will move faster either left or right. Also the key which is working slower at the time will have slight lag before moving the object. Later I found out it will move faster if moving towards currentRotation value of zero. So if currentRotation is lets say 100 and I hold left key (which reduces that number) it will move faster than if using the right key. And if currentRotation is lets say -100 then the right key (which increases the value towards zero) will make it move faster.

    You may have guessed by now that I'm not good with mathematics and geometry, but what I know for sure is that the problem is definitely not in some phantom input or somewhere else in code, I have spent weeks now analyzing and checking everything. It is quite clear that the object is moving faster when the currentRotation value is moving towards zero, so the problem must be in the method that I'm using, so maybe someone here may shed some light on this issue.

    Code (JavaScript):
    1. function Update () {
    2.  
    3. var steerPower:float = Input.GetAxis("Horizontal");
    4.  
    5. steerPower *= (130*Time.deltaTime);
    6.  
    7. currentRotation += steerPower;
    8.  
    9. rotation.eulerAngles = Vector3(0, 0, currentRotation);
    10. transform.position = rotation * radius;
    11.  
    12. //result is the movement will be faster and lag-free when the value of currentRotation is moving towards zero, and will be slow to start and move slower if the same value is moving away from zero
    13.  
    14. }
    Please help... Been stuck with this for a while, and is preventing me from making the game controls feel as responsive as they should.
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    There are two things I would like to know.... What is rotation? is it a quaternion or a vector?

    Two, what are you trying to control. This seems like an odd behavior to try to control like this. Is it a space ship or a car?

    If I am reading it correctly, you are rotating around a planet, or something like that.
     
    kr4ft3r likes this.
  3. kr4ft3r

    kr4ft3r

    Joined:
    Feb 18, 2014
    Posts:
    10
    Wow thanks, you just helped me get rid of it. The rotation is declared as Quaternion, but that's not the problem, next to it I found private var currentRotation = 270.0; declared without type, setting it to float fixed the thing. I wrote that class too long ago, never bothered to recheck for basic noob mistakes. What a magical moment, my mysterious circular moving thingy is behaving as expected now.