Open ArtBIT opened 3 years ago
try this following code. it works for me. I get this idea from https://www.youtube.com/watch?v=saAQNRSYU9k&ab_channel=SebastianLague user: TheBeLuvdTRex 's comment. I leave this following code in his comment too.
void Update()
{
if (pathCreator != null)
{
distanceTravelled += speed * Time.deltaTime;
transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, endOfPathInstruction);
//transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled, endOfPathInstruction);
Quaternion angle = pathCreator.path.GetRotationAtDistance(distanceTravelled);
angle.x = 0f;
angle.y = 0f;
transform.rotation = angle;
}
}
Thanks @ChenQTYF, though I've already left a working solution in my original comment, it's always great to have an alternative.
The reason why I created this Issue was to inform the author that a workaround is needed in the first place. It would be great if this could be handled at the GetRotationAtDistance call itself, where it could check if we are in a 2D mode, are we using XY, or XZ plane, and rotate the result accordingly before returning it to the user.
[EDIT]
It seems that the library already does exactly this, under the hood. There's a MathUtility.TransformDirection
helper that should be doing this.
Agreed, MathUtility.TransformDirection doesn't seem to return the correct result in the 2D scenarios, despite appearing to have code for this at a quick glance. Unless perhaps my setup is wrong in some way. The solutions above work or more generally you can modify VertexPath.GetRotation to the Quaternion rotation
value before returning:
if (this.space == PathSpace.xy) {
rotation.x = 0f;
rotation.y = 0f;
} else if (this.space == PathSpace.xz) {
rotation.x = 0f;
rotation.z = 0f;
}
Excellent, thanks @ArtBIT - Although in my case, I had to use Vector3.up
opposed to Vector3.right
. ❤️
If I try to follow the examples and use the path rotation at distance for 2D sprites, the sprite disappears.
This is what I had to do to achieve the desired behavior for 2D objects: