(unity) Quaternion.RotateTowards doesn't seem to work as I imagine it to?

I'm trying to get something like this going:

Given point A, given a transform with an arbitrary forward (it can turn), and given the starting position of an object anchored at point A, I want to rotate that object away from the forward. I googled around and found Quaternion.RotateTowards, which seemed right:

Object.MoveRotation(Quaternion.RotateTowards(Quaternion.Euler(Object.x, Object.y, Object.z), pointA.rotation, x));

But after testing it out, it doesn't actually do what I want it to do, the resulting angle changes according to how bit x is, if it's close to zero, it basically becomes the same as the original Object.x, if it's positive high, it replicates the forward, if it's negative, it seemingly goes randomly. Am I using it correctly?

Is there an easier way to achieve something like this?? (In 3D, not 2D)
Screenshot 2019-02-10 at 08.11.19.png
421 x 365 - 30K

Comments

  • edited
    Transform.LookAt() – Points the Z-axis at a position in world space
    Quaternion.LookRotation() – Points the Z-axis in a direction
    Quaternion.SetFromToRotation() – From one direction to another
    Quaternion.RotateTowards() – Match rotation

    It sounds like you want Quaterion.SetFromToRotation?
  • I looked at Quaterion.SetFromToRotation and I'm not sure how to work it - it says it "Creates a rotation which rotates from fromDirection to toDirection." - but a quaternion is one single representation of rotation in space, how is it representing both a from and a to at the same time?

    Also, I'm unsure of what the "to" direction is, and therefore I thought RotateTowards would work as it is theoretically "rotate towards a target but stop when you've rotated x degrees", which seems like what I want. I don't know the exact target but I do know the target I want to rotate towards and how many degrees i want to rotate it towards by (not all the way).
  • Honestly, I can’t even tell you about this.
  • edited
    If Object is a child of A, positioned as in your sample image then rotating A would cause the "door open/close" like action I see in the image. It is then as simple as a.rotation = Quaternion.Euler(0, a.rotation.y + something, 0);

    Of course if Object's pivot was at point A to start with then it does not need to have the parent to act as anchor.

    But maybe I am not understanding the question correctly.
  • Not sure if you sorted it out (a few days old) but if you know the target rotation you can multiply Quaternions.

    So you can get the world rotation to your target with:

    Quaternion Rot = Quaternion.LookRotation(Target.position - transform.position, Vector3.up);

    then alter the rotation using something like:

    Quaternion Offset_Rot = Rot * Quaternion.euler(0f,somedegreesinY,0f);


  • @Leslie can't use parenting and stuff because that messes with physics. I'm using physics to simulate everything. Also it's in 3D so XYZ axis must all be considered.

    @Blackson I know the target's physical location in space that I want to rotate towards, not sure if that's what you mean?
    I can't just pick one set of degrees and ignore X and Z as I'm moving in 3D space and not just 2D.
  • edited
    If you want the object to look at some other object in the scene then it should be as simple as `transform.LookAt(otherTransform);`

    nvm, I see you made a new post with more info about this.
  • If you want to rotate towards a point in space use:

    Quaternion TargetRot = Quaternion.LookRotation(Target.position - transform.position, Vector3.up);
    (that gives you the final target rotation)

    Then lerp the rotation of the object that must look with:
    transform.rotation = Quaternion.Lerp(transform.rotation,TargetRot,Time.deltatime*rotation speed);



Sign In or Register to comment.