- Home /
Proper Variation of this code, to work with any UP normal;
if(newVelocity.magnitude > 0) {
Vector3 up = transform.up;
Vector3 desiredLookPos = seekPos;
desiredLookPos.y = transform.position.y;
Vector3 desiredLookDir = desiredLookPos - transform.position;
desiredLookDir.Normalize();
float rotationDeltaInRadians = m_rotationSpeed * Time.deltaTime;
Vector3 newForwardDirection = Vector3.RotateTowards(
transform.forward,
desiredLookDir,
rotationDeltaInRadians,
0.0f
);
transform.forward = newForwardDirection;
transform.up = up;
}
So, basically the above code works all fine and well, minus the transform.up located at the bottom, was a temperary fix to something;
anyways, basically the character is suppose to rotate towards the direction of a point, and its working quite well, however I come to a point where my character has an upside down UP axis -1, instead of 1, and basically, that messes everything up; because now he rotates the opposite direction while upside down, where as when up-right he doesnt rotate at all(at least since I added the transform.up = up; )
is there anyone on here who can help me out of this pickle, and get me the right calculations to make him rotate problem, no matter if his UP normal is 0,1,0; or 0,-1,0 ?? please!
would be a great thing, and I suppose I can even offer a $5 reward.
Just to be sure - you are sure that newVelocity.magnitude is > 0? m_rotationSpeed is sensible etc and it doesn't work even if max change in magnitude is set to a large value (as your magnitude isn't changing anyway, just to test).
he rotates fine.. just not when I set his up axis to 0,-1,0; then he rotates oppositely, otherwise he does not rotate at all.
Answer by N1nja · Jun 18, 2012 at 10:17 PM
var up = transform.up;
var lookRot = Quaternion.LookRotation((seekPos - transform.position)*up.y);
var axisAngles =transform.rotation.eulerAngles;
axisAngles.y = Quaternion.RotateTowards(transform.rotation, lookRot, (m_rotationSpeed * Mathf.Rad2Deg * Time.deltaTime)).eulerAngles.y;
transform.rotation = Quaternion.Euler(axisAngles);
this appears to have fixed it. Mike was the closest, so Mike, if you read this again, let me know and I will give you $5.00 :D Just need your paypal email.
Hey man, don't need your cash, just glad you solved the problem :)
fine. i do not need to give it to you then. xP
thanks for the input, u just had to move that * up.y up a few lines.. don't ask why I thought to do that, other then I knew that would also invert the direction from the start, hoping that'd be enough.
turns out it was. :P
Answer by whydoidoit · Jun 18, 2012 at 09:01 PM
var up = transform.up;
var lookRot = Quaterion.LookRotation(desiredLookPos - transform.position);
var axisAngles =transform.rotation.eulerAngles;
axisAngles.y = Quaternion.RotateTowards(transform.rotation, lookRot, (m_rotationSpeed * Mathf.Rad2Deg * Time.deltaTime)).eulerAngles.y;
transform.rotation = Quaterion.Euler(axisAngles);
$$anonymous$$uch closer, however while upside down, he no longer looks forward at me, he turns around and his back looks forward at me, this was the original problem, that I am trying to fix. Good though.
There has to be a better way than this :)
var up = transform.up;
var lookRot = Quaterion.LookRotation(desiredLookPos - transform.position);
var axisAngles =transform.rotation.eulerAngles;
axisAngles.y = Quaternion.RotateTowards(transform.rotation, lookRot, (m_rotationSpeed * $$anonymous$$athf.Rad2Deg * Time.deltaTime)).eulerAngles.y;
transform.rotation = Quaterion.Euler(axisAngles);
transform.forward = transform.forward * up.y;
i've tried the transform.forward * up.y myself, it doesn't work either.
This is a much harder problem then it seems like, trust me, I've tried most of these solution you've all posted. They are not working.
This may take some outside thinking.
I guess I can't visualize the effect you after. I'm not sure what the existing rotations in X & Z are about. Can you describe the effect in terms of what your objects are doing? I guess you not being able to fix it with all the code and the idea is going to make it tricky for us with just a few lines of code and a guess at what you want ;) Does the character flip vertically by rotating around Z?
Answer by aldonaletto · Jun 18, 2012 at 06:38 PM
I don't know why it's rotating to the wrong side when the object is upside down, but probably the version below may solve the problem - it uses Quaternion.RotateTowards instead of Vector3.RotateTowards, thus can be applied directly to the object rotation:
if(newVelocity.magnitude > 0) { Vector3 desiredLookPos = seekPos; desiredLookPos.y = transform.position.y; // kill any height difference // find the desired rotation Quaternion lookRot = Quaternion.LookRotation(desiredLookPos - transform.position); // and rotate a little in its direction each pass transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRot, m_rotationSpeed * Time.deltaTime); }Notice that m_rotationSpeed in this case is expressed in degrees per second (don't know if the previous one was in radians/second)
EDITED: My bad! Quaternion.LookDir really doesn't exist: it should be Quaternion.LookRotation - answer fixed.
Im sure this may work if Quaternion.LookDir was a real function, did you mean setLookRotation?
I changed it to LookRotation, and it acts a little more reasonably, however hes rotation on the Z axis and X axis, how-come? Hard to say if its correct due to that, can you write it so it ensures he only rotates on the Y-axis, and in any UP normal?
also provide an email to paypal, if it works as expected, you will receive $5.00
Hang on - you only want him to rotate about Y? You didn't mention that :)
var lookRot = Quaterion.LookRotation(desiredLookPos - transform.position);
var axisAngles =Quaternion.RotateTowards(transform.rotation, lookRot, (m_rotationSpeed * $$anonymous$$athf.Rad2Deg * Time.deltaTime)).eulerAngles;
axisAngles.x = 0;
axisAngles.z = 0;
transform.rotation = Quaterion.Euler(axisAngles);
The above rotates correctly, and only one the Y axis, as requested, but again, it does not keep the same UP Normal, as defined before the rotation occures. For instance before rotation Up Normal = [0, -1, 0]; after [0, 1, 0]; Where that shouldn't happen after, it should remain as before.
Providing an correct script, and email, will give you $5.00 first to do it right.
Oh does it does, doesn't it - does it work if you just remove the axisAngles.z = 0
or perhaps it is more complicated than that...