SebLague / Path-Creator

Path creation asset for Unity game development
https://assetstore.unity.com/packages/tools/utilities/b-zier-path-creator-136082
MIT License
1.83k stars 319 forks source link

Applying rotation to 2D sprite makes it dissapear #109

Open ArtBIT opened 3 years ago

ArtBIT commented 3 years ago

If I try to follow the examples and use the path rotation at distance for 2D sprites, the sprite disappears.

transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled);

This is what I had to do to achieve the desired behavior for 2D objects:

transform.rotation = Quaternion.FromToRotation(Vector3.right, pathCreator.path.GetNormalAtDistance(distanceTravelled));
ChenQTYF commented 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;
            }
        }
ArtBIT commented 3 years ago

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.

brenden-t-r commented 2 years ago

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;
}
ShaneYu commented 1 year ago

Excellent, thanks @ArtBIT - Although in my case, I had to use Vector3.up opposed to Vector3.right. ❤️