yasirkula / UnityBezierSolution

A bezier spline solution for Unity 3D with some utility functions (like travelling the spline with constant speed/time)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rotation when flying a looping

TrickShotMLG02 opened this issue · comments

commented

I am using your asset for a flight path. But when I want to fly a looping, the plane rotates when flying straigth up and also when flying straight down. Is there any possibility to fly a looping overhead?

Probably caused by Quaternion.LookRotation's upwards parameter always pointing up. Perhaps changing this line as follows would work:

{
	Quaternion _rotation1 = Quaternion.LookRotation( spline.GetTangent( m_normalizedT ), Vector3.up );
	Quaternion _rotation2 = Quaternion.LookRotation( spline.GetTangent( m_normalizedT ), Vector3.down );
	
	targetRotation = Quaternion.Angle( _rotation1, transform.rotation ) < Quaternion.Angle( _rotation2, transform.rotation ) ? _rotation1 : _rotation2;
}

Alternatively, you can use the bezier points' Extra Data property to tell whether or not the plane should be upwards or downwards at a section of the spline. At the upper section of the loop, bezier points' Extra Data would have a negative component:

ss

Inside the BezierWalker script, if this value is negative, we can use Vector3.down as upwards parameter:

targetRotation = Quaternion.LookRotation( spline.GetTangent( m_normalizedT ), spline.GetExtraData( m_normalizedT ).x >= 0 ? Vector3.up : Vector3.down );
commented

Thank you very much for your fast reply. Everything works well now

Great work, keep it up!