How do I make an object face forward while it is orbiting another object?

I have a simple enemy in my game which orbits the player if it comes within a certain distance of them, the enemy orbits correctly and everything else carries on correctly. But I cannot figure out how to make the enemy appear that it is facing forward, so the enemy rotates facing the player and looking very strange.

So how can I make the enemy face the way it is moving while orbiting?

Here is the movement section of my code

using UnityEngine;
using System.Collections;

public class MineLayerAI : MonoBehaviour {

	public GameObject target;
	public Transform Target;
	public float rotationSpeed = 3.5F;
	public float moveSpeed = 3.5F;
	public Transform enemyTransform;
	public Rigidbody MinePrefab;
	public float WaitLayTime = 2.0f;
	public bool LayTime = false;
	public float OrbitSpeed = 50.0f;
	public bool wallHit = true;
	
	
	void Awake(){
		
		enemyTransform = transform;
		
		if(!target)
			{
				target = GameObject.FindWithTag("Player");
			}
		if(!Target)
			{
				Target = GameObject.FindWithTag("Player").transform;
			}
		
	}
	
	void Update () 
	{
		
		
		float currentDistance = Vector3.Distance(transform.position, Target.position);
		
		if(currentDistance > 2.5)
			{
				//rotate towards player
				enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.transform.position - enemyTransform.position), rotationSpeed*Time.deltaTime);
				//move towards player
				enemyTransform.position += enemyTransform.forward * moveSpeed * Time.deltaTime;
			}
		else
			{
				if(wallHit == true)
				{
					//rotate towards player
					enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.transform.position - enemyTransform.position), rotationSpeed*Time.deltaTime);
					//orbits around the player
					transform.RotateAround (Target.position, Vector3.up, OrbitSpeed * Time.deltaTime);	
				}
				if(wallHit == false)
				{
					//rotate towards player
					enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.transform.position - enemyTransform.position), rotationSpeed*Time.deltaTime);
					//orbits around the player
					transform.RotateAround (Target.position, Vector3.down, OrbitSpeed * Time.deltaTime);
				}
				 
			}

Thank you in advance for any help

Jack Perry

I’m having trouble sorting out the specific of the here to write you the exact line of code you need. A general solution is to use the cross product of a vector between the center and the orbiting object and the axis of rotation. So if the axis of rotation is the ‘Y’ axis, then the code might look like this (from a script on the orbiting object):

var lookDir = Vector3.Cross(transform.position - center.position, Vector3.up);
transform.rotation = Quaternion.LookRotation(lookDir, Vector3.up);