Path-Creator icon indicating copy to clipboard operation
Path-Creator copied to clipboard

Normals on 2D line cause unwanted rotation

Open Mosscairn opened this issue 6 years ago • 4 comments

Screen Shot 2019-09-22 at 8 22 10 AM This is the 3D scene view of the moving object, it's rotation is set to 0 on every axis by default. The normals of the line cause it to rotate on the X and Y axis, making it somewhat useless for 2D projects. Is there any way to make the normals face in the direction the line is going in 2D? Hopefully I'm just doing something wrong.

Mosscairn avatar Sep 22 '19 15:09 Mosscairn

I've been having issues with normals on 3D splines too. Let's say I want the first and last nodes' normals to both point up. 'Up' on the first node might be 90 degrees, but on the last node, 90 degrees is not up; I have to eyeball it to 326.3 degrees by trial and error. Why is this? It makes it impossible to ensure a follower starts and ends in the same orientation.

alec-netsphere avatar Oct 01 '19 12:10 alec-netsphere

got the same issue on my XY 2D game. looks like sprites are rotated 90° on the Y axis, making the GetRotationAtDistance unusable for 2D projects. I'll try to figure out what is wrong the computation (or in my code).

guilhemvors avatar Oct 17 '19 21:10 guilhemvors

I have figured out what is happening. In the VertexPath, there is the method that handles the rotation:

        /// Gets a rotation that will orient an object in the direction of the path at this point, with local up point along the path's normal
        public Quaternion GetRotation (float t, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop) {
            var data = CalculatePercentOnPathData (t, endOfPathInstruction);
            Vector3 direction = Vector3.Lerp (localTangents[data.previousIndex], localTangents[data.nextIndex], data.percentBetweenIndices);
            Vector3 normal = Vector3.Lerp (localNormals[data.previousIndex], localNormals[data.nextIndex], data.percentBetweenIndices);
            return Quaternion.LookRotation (MathUtility.TransformDirection (direction, transform, space), MathUtility.TransformDirection (normal, transform, space));
        }

The problem when working in 2D is that the LookRotation method (as per The Doc) rotates the object so that the Z-axis is looking into the given direction.

While it is maybe what the author want for 3D object, for 2D, it doesn't work.

As a workaround you can always rotate the object back into its right orientation by adding an extra * Quaternion.Euler(0, -90, 0); after the GetRotationAtDistance method:

transform.rotation = curve.path.GetRotationAtDistance(distanceTravelled, end) * Quaternion.Euler(0, -90, 0);

guilhemvors avatar Oct 17 '19 22:10 guilhemvors

I'm a bit late to this discussion but I've still got some problems with the rotation on 2d. Without the * Quaternion.Euler(0, -90, 0); my player character seems to 'teleport' to a random location instead of following the path. After adding the * Quaternion.Euler(0, -90, 0); the character does follow the path, however, the entire background image starts turning instead of the character itself. Any suggestions?

ZenyteFury avatar Feb 02 '21 10:02 ZenyteFury