Quaternion Slerp 180 Away from target

I am working on a space flight simulator, at this point i’m trying to get an autopilot running to approach and stop at a defined distance to the target object.

I don’t want the possibility to simply brake down the velocity to zero, in case the ship has to stop accelerating, turn arround 180 degrees by a defined turnrate and then start the engines to slow down until it reaches a velocity near zero were it can brake to zero an turn around again to face the target.

	void Update () {
	
	if(Input.GetKeyDown(_control.autoApproach) && !_approach){
		if(_target.selectedTarget != null){
			_target.approachingTarget = _target.selectedTarget;
			_distance = Vector3.Distance(_target.approachingTarget.transform.position, _myTransform.position);
			_way = _distance - _maxDistance;
			
			_approach = true;
		}
		else
			_approach = false;
	}
	if(Input.GetKeyDown(_control.stopAutopilot)){
		_approach = false;
		_MoObj.DefEngineOutput = 0;
	}
	
	if(_approach){			//if approach
		if(!_braking)
			RotateToTarget();	//turn to face target
		Vector3 directionToTarget = _myTransform.position - _myTargetTransform.position;
		float _angle = Vector3.Angle(_myTransform.forward, directionToTarget);
		if (_angle > 179 && _angle < 181){
			MoveToTarget();		//approach till < 2000
		}
		if(_rotateToBrake){
			RotateToBrake();	//Turn around 180°
		}						
		if(_braking){			//Increase Output to slow Ship down
			_distance = Vector3.Distance(_target.approachingTarget.transform.position, _myTransform.position);					//CheckDistance
		}	
								
								//Decrease Output to decrease Speed before arriving at TargetVector
								//Arrive at TargetVector by Brake();
								//Rotate to View Target
	}
}

	private void RotateToTarget(){
	if(_target.approachingTarget != null){
		_myTargetTransform = _target.approachingTarget.transform;
		_myTransform.rotation = Quaternion.Slerp(_myTransform.rotation, Quaternion.LookRotation(_myTargetTransform.position - _myTransform.position), _MoObj.TurnRate * Time.deltaTime);
	}
	else{
		_target.approachingTarget = _target.selectedTarget;
		RotateToTarget();
	}
}

	private void RotateToBrake(){
	
		
	if(_target.approachingTarget != null){

		_myTargetTransform = _target.approachingTarget.transform;
		_myTransform.rotation = Quaternion.Slerp(_myTransform.rotation, Quaternion.Inverse(_myTransform.rotation), _MoObj.TurnRate * Time.deltaTime);
		//_myTransform.Rotate(Vector3.down, _MoObj.TurnRate);	 
		
		
		
		
	}
	else{
		Debug.Log("No Target for AutoPilot!");
	}
}

My actual problem lies in the RotateToBrake() function.
I don’t get it and its late now.
I checked nearly ever type of rotate function Unity comes up with, i tried a 100 times to print out myTransforms.rotation, viewports, rigidbody rotations i tried euler angles, Quaternions, defined angles, simple Transform.Rotate to AddForce Variants.
I tried to set a new Gameobject directly behind my ship to use turnto again but somehow i made it wrong.
Maybe some1 has an idea how to get the exact direction behind my ship and turn it there in a smooth way like a Quaternion.Slerp();

Or even a completly different Idea how to solve the hole thing…

Greetings Mahtrok

1 Answer

1

Look into Transform.forward. You probably want to negate that to make it ‘backward’. Then slerp or RotateTo that. If you want it to ‘ease’ to a stop, check this out: unifycommunity.com