#Hi everyone, I'm trying to rotate an

1 messages · Page 1 of 1 (latest)

wise locust
#

what are you actually trying to do? this looks like some kind of ad hoc IK code

fallen kite
#

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;````
wise locust
#

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

fallen kite
#

you might suprise yourself

wise locust
#

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

fallen kite
#

yeap

#

or you already know how to do it

wise locust
#

i feel like the order issue rarely comes up

#

because people who don't understand the quaternion commutativity issue try every order anyway

fallen kite
#

@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

valid yarrow
#

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

valid yarrow
#

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 😭

wise locust
#

that's kind of problem number 1

#

like what is this for?

valid yarrow
# wise locust 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

wise locust
#

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

valid yarrow
fallen kite
#

btw have you tried using the examples unity have for procedural walking?

#

they have a procedural walking robot

#

example

wise locust
#

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

valid yarrow
valid yarrow
wise locust
#

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

valid yarrow
#

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?

valid yarrow
#

Thanks, I'll try it out tomorrow