Search Unity

EulerAngles: That's not the turn I wanted!

Discussion in 'Scripting' started by cscxfjug, Sep 9, 2018.

  1. cscxfjug

    cscxfjug

    Joined:
    Aug 6, 2018
    Posts:
    77
    Code (CSharp):
    1.  
    Context is not needed in that regard.
    When I press and hold A. Debug.Log returns following:
    0.001131228 / 325 / 0.001345537 (it continues to push random low numbers very close to these on X and Z)
    I expect, X rotation 0.001, Y rotation 325, Z rotation 0.001
    I want, X rotation dead 0, Y rotation 325, Z rotation dead 0
    I get:
    transform.png
    Rotation X and Z, do not change in game.
    Position X, Y and Z, have random spasms, from (I think) -5.0f to 5.0f.
    Rotation Y, does not change, and it is stuck on 1.131, like it should be on 325 if everything worked.
    This is an empty GameObject with Rigidbody, attached with FixedJoint to rest of the body.
    There's no other interaction with this object, other than Rigidbody.AddForce().
    There's no other mention of this GameObject.

    That's all I could tell you, that's all I can think of. If you need more information just ask.

    So how do I turn an object again?

    P.S.: The screenshot and the Debug.Log output is from MotorOne.
     
    Last edited: Sep 13, 2018
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    At a high level, the 2 main paths open to you are (1) the physics engine or (2) manually slerping the rotation.

    For (1), you could take a look at AddTorque.

    For (2), you could take a look at RotateTowards.

    Either way, it is highly recommended to brush up on rotation handling in Unity. That is invaluable reading as handling angles always seems to be a little nuanced. :)
     
    cscxfjug likes this.
  3. cscxfjug

    cscxfjug

    Joined:
    Aug 6, 2018
    Posts:
    77
    I tried AddTorque on a cube demo, and it doesn't seem to allow me locking in position. I don't want it to exceed maximum rotation of, say 35 degrees. It is a continuous unrestricted rotation

    Then I just looked at RotateTowards, which needs me to put a GameObject that I have rotate my object [MotorOne] towards. I have a problem that in order for this to work. I need to create an invisible self-rotating GameObject, which will react to my input and orbit around the MotorOne. That will bring me to exactly same problem I have right now.

    Regardless of it [MotorOne] having a Rigidbody, the motor shouldn't be rotated by physics or others. I need ability to force rotation on it and tell it "this is the rotation, I don't care where and what you are, rotate this, snap instantly". It shouldn't rely on physical engine (I have disabled gravity and box collider) or external object files. I should be able to directly demand its rotation.

    Is there really no other way?
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Oh, so you just want to set a rotation directly? Then just use LookAt. You just need the target position (which you can provide as a GameObject or just calculate the transform yourself if you like).
     
    cscxfjug likes this.
  5. cscxfjug

    cscxfjug

    Joined:
    Aug 6, 2018
    Posts:
    77
    I wish that would work. I had made some changes though non-crucial to the error. I calculated Vector3.Lerp of two objects to calculate position, to which my item should LookAt. I even created a test cube, that changed its position to the same position. Whilst that test cube followed the location perfectly, when I told my object to LookAt this position, it jerked in a different but same way as in eulerAngles, different numbers, maybe frequency, but was equally unusable. It spastically changed it's position and its rotation was nowhere near accurate.

    There are no other lines of code that attempt to change it's Transform.

    What would you recommend now? Is there any information I could provide you?
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Are you manually rotating an object that's attached to a physically active joint?
     
    cscxfjug likes this.
  7. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Your test of creating a target cube is a very good approach to solving problems like this.

    So, taking one step further down that path, if you create a new GameObject, place a Cube on it and also your "LookAt" script, does this new cube track the moving test cube correctly now?

    If not, then there is almost certainly an error in the script and you could paste the code here if you like. If it does track correctly, then, as GroZZleR suggests, there must be something else at play on your original object.
     
    cscxfjug likes this.
  8. DoublePixelStudio

    DoublePixelStudio

    Joined:
    Jan 30, 2017
    Posts:
    69
    Rotatetowards
     
  9. cscxfjug

    cscxfjug

    Joined:
    Aug 6, 2018
    Posts:
    77
    Yes, that's the one. Literally five minutes ago I realized it's attached and found out it literally locks it in rotation and position of its connected body (I thought it was just forcing its transform.position, not rotation). Very stupid of me. I swear I'm not an idiot.

    I guess the question right now is: Is there a way, to have same effect as FixedJoint, but allow it to turn? Is there another type of special Joint with special setting that would allow it turn according to my demands but yet, be connected to my special Rigidbody (it's parent object) and exert force on it (I will exert force onto object itself, which by being connected to its parent, will force it forward)?

    I'm sorry for wasting your time. I should've thought about this.

    Edit: ConfigurableJoint? That's bit much to me. I'll try it though.
    Edit2: Why does ConfigurableJoint automatically rotate my item without input?
     
    Last edited: Sep 18, 2018
  10. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    To be honest, I'm not entirely clear on what the end goal is. You can't have an object be a physically active joint and be driven by manual movements and rotations at the same time.

    Your best bet is to make the visual representation of your object a child of the physically active object and manually set the rotation of the visual representation, while letting the physics drive the parent object. That may cause some disconnect between where the colliders are and where the player sees them as being, but again, I'm not really sure of your goals here.
     
    Doug_B and DoublePixelStudio like this.