#Hi everyone, I'm trying to rotate an
1 messages · Page 1 of 1 (latest)
The issue you're experiencing might be due to the order of multiplication of the quaternions. Quaternion multiplication is not commutative, meaning that the order in which you multiply them matters.
In your case, you're first applying the normalRotation and then the yRotation. This means that the yRotation is applied in the local space of the object after it has been rotated by the normalRotation.
If you want the yRotation to be applied in world space, you should multiply it first. Here's how you can modify your code:
This will first apply the yRotation in world space, and then apply the normalRotation in the local space of the object.
If this doesn't solve your issue, it might be helpful to debug your rotations separately to make sure they're working as expected. You can also try using Quaternion.FromToRotation instead of Quaternion.LookRotation to calculate the normalRotation.
// Combine the normal rotation and the Y-axis rotation
Quaternion combinedRotation = yRotation * normalRotation;
transform.rotation = combinedRotation;````
without knowing what the goal is the bot isn't going to give a very useful answer
the issue isn't combining the rotations, it's like, doing what the user actually wants
it's true that the order matters and that this is an error though
you might suprise yourself
not for this
i mean all of what the user has written is incorrect*
it's better to ask first how to achieve the goal
i feel like the order issue rarely comes up
because people who don't understand the quaternion commutativity issue try every order anyway
@valid yarrow try this code
//Calculate the rotation based on the Y axis rotation
Quaternion yRotation = Quaternion.Euler(0, Time.deltaTime * currentAngularVelocity, 0);
//Calculate normal
Vector3 v1 = rightLegTargets[0].transform.position - leftLegTargets[^1].transform.position;
Vector3 v2 = rightLegTargets[^1].transform.position - leftLegTargets[0].transform.position;
Vector3 normal = Vector3.Cross(v1, v2).normalized;
//Calculate rotation to align transform.up with the normal vector
Quaternion normalRotation = Quaternion.FromToRotation(transform.up, normal);
//Combine the normal rotation and the Y-axis rotation
Quaternion combinedRotation = yRotation * normalRotation;
transform.rotation = combinedRotation;````
man I dont know jack shit of quaternions
I just want to know if this one would work xD
but maybe because is gona work
because this is correct I think
```Quaternion.FromToRotation(transform.up, normal);````
he was not using the transform.up
is using the .foward
Hi, I managed to make something work, and yes I am working on a procedural animation script.
Heres the code if youre interested:
//Calculate the rotation amount based on angular velocity
float rotationAmount = currentAngularVelocity * Time.deltaTime;
//Adjust the transform.up vector to be dependent on the surface normal
Vector3 v1 = rightLegTargets[0].GetComponent<LegStepper>().homeTransform.position - leftLegTargets[^1].GetComponent<LegStepper>().homeTransform.position;
Vector3 v2 = rightLegTargets[^1].GetComponent<LegStepper>().homeTransform.position - leftLegTargets[0].GetComponent<LegStepper>().homeTransform.position;
Vector3 normal = Vector3.Cross(v1, v2).normalized;
Vector3 slowlyTowardTarget = Vector3.Slerp(transform.forward, new Vector3(towardTarget.x, transform.position.y, towardTarget.z), 1 - Mathf.Exp(rotationAmount)).normalized;
//Interpolate between the current rotation and the rotation with adjusted up vector
transform.rotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(slowlyTowardTarget, normal), normal);
I would rather do it as a 2 step rotation to make it more scalable, but I didnt really manage to do that. I'll look into the suggestions here and try to implement that later
I tried this and not only does it not rotate around the y axis, when it goes on a slope the object goes crazy jittery and all over the place
Also after further testing, my solution can cause problems too sometimes so I'm pretty much out of ideas
All I need is for my object to rotate around the y axis and align with a normal why is that so difficult unity ðŸ˜
you haven't quite said what you want to do
that's kind of problem number 1
like what is this for?
I'm making a procedural animation controller script that works with a dynamic amount of legs and multiple gaits. Basically I need a spider or an ant for example to be able to walk on slopes without looking weird, which means to rotate their body to the normal which is calculated by the tip of the leg positions
why are you not using an IK solution for this?
can you describe in words how you want the body to behave?
have you watched a video of a spider or ant walking? they are segmented animals. so it doesn't make sense that there's only "one" body part moving, they will have 3
nothing has locomotion with a rigid waist
imo the body pieces move with logical gameplay-like motion, and then you place the legs, rather than the other way around
Maybe I didn't explain myself clearly enough
I am using an IK solution, and in my script im only moving the body that holds the legs. Im moving the body and the legs move with it using procedural animation
btw have you tried using the examples unity have for procedural walking?
they have a procedural walking robot
example
okay. simply do
transform.up = targetNormal;
that's it.
if you want to animate the body's normal, Vector3.Lerp small deltas.
you can compute the target normal however you want. average of the front feet's normals? sure. whatever.
don't overcomplicate that. you can do it in 2 lines of linq. chatgpt can tell you how
something like average = feetNormals.Aggregate(Vector3.zero, (a,b) => a+b) * (1f / feetNormals.Length);
but animation rigging already supports all this automatically
it does the same thing
you don't need to be doing this in code at all
I haven't actually, might check that out later
It's problematic to do it this way as it completely overrides and prevents transform movement from my experience with doing it this way. This means im forced to use rigidbody to move my object and I might need to use navmesh agents later which use transform movement I'm pretty sure
use animation rigging
setting transform.up isn't any different than rotating
animation rigging will let you express the relationship between the body and the feet in an ergonomic way
What do you mean by "use animation rigging"? I'm already using it as an IK solver to move the legs
Is there more to it that I'm missing?
the animation rigging package?
damp the rotation
that kind of looks like it's what you want to do
Thanks, I'll try it out tomorrow