- Home /
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
U could use Transform.LookAt(). Then u specify a vector
Im not sure how I would get the vector though, as it will be constantly changing.
Answer by robertbu · Apr 26, 2014 at 07:41 PM
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);
Edit: I have realised that I just needed to swap the centre.position and the transform position around.
Hey, so that does work for the rotation, the only thing is it seems to be rotating in reverse.
Why would it be doing that?
Apologies for more questions, but is there any way to make the transition from movement to rotation smooth?
If your object is facing 180 degrees the wrong way, either 1) swap the order of the two parameters in the Vector3.Cross or 2) netage the axis in the Vector3.Cross(), or 3) negate the result of the Vector3.Cross(). All three should flip the look direction 180 degrees. I don't have an answer for your smooth transition issue.
Thank you very much for your help, I'll see what else I can dig up on smoothing the transition. Thanks again, I've been banging my head about that for days.
Your answer
Follow this Question
Related Questions
Transform.Rotate producing unexpected results when being used after setting localEulerAngles 0 Answers
Rotating a boat on two axes with force. 2 Answers
Third Person Camera Orbit 0 Answers
Rotate an object relative to Camera 2 Answers
Transform rotation and position on key input: rotation only working the first time 2 Answers