# Look at target while moving around a sphere

Hi,

I can’t figure out how to have my enemy look in the direction of the player, but in the “enemy space”, so that the enemy looks forward while moving, and not down through the sphere. So basically converting the green line to rotate the enemy in that direction in its own space. I use transform.forward to move the enemy.
This is on a sphere, so it has to work no matter the rotation of the enemy.

My best guess was taking the player direction, converting it to enemy local space, set y to zero in local space, and convert it back to global space. Then use LookAt to rotate the player. But it doesn’t quite work.

Wait until the enemy can see the player before having it look at the player.

that’s not gonna work unfortunately.
The enemy is seeking the player even when not in sight, and the enemies will most likely be in view because of the terrain and camera setup.

Quaternion targetRotation = Quaternion.LookRotation(movementDirection);

``````    void Update()
{
Vector3 dir=transform.forward; // get our current direction
float angle=Vector3.SignedAngle(dir,player.position,transform.position.normalized); // get the angle of the player in relation to our direction on the planet
dir=Quaternion.AngleAxis(angle,transform.position.normalized)*dir; // rotate our forward direction so that it points in the shortest direction to the player
transform.rotation=Quaternion.Lerp(transform.rotation,Quaternion.LookRotation(dir,transform.position.normalized),4*Time.deltaTime); // gradually look towards the direction
}
``````

This will look in the shortest direction to the player and not directly through the planet.

Hey zulo3d

it works great when the player and enemy are on the surface of the planet. Things is they can move to the inside of the planet, and then the enemy gets confused. cant figure this out.
gif attached

There is a gravity script that sticks objects to the ground/loft. adding it just in case that could be the culprit.

``````    void Update()
{
if (Physics.Raycast(transform.position, -transform.up * 5, out RaycastHit hit, 30, layerMask))
{
Quaternion targetRotation = Quaternion.FromToRotation(transform.up, hit.normal.normalized) * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, slerpValue);
}
}

private void FixedUpdate()
{
}
``````

Also, i’m wondering if there is a better way of getting this to work. The player can walk outside and inside the planet, and the enemies should follow. When the player and enemy are not both on the same side, i set the enemy to target a point at the hole until it’s on the same side, and then targets the player.

The enemy and player being on the different surfaces (inner/outer) is always going to be an issue for you because there’s no simple way for the enemy to find a path through the holes in the surface. You’re going to have to find a path finding solution that can handle complex meshes.

This code below will use the enemy’s current up direction to get the angle of the player which will be better suited to your asteroid. Although it still won’t solve your path finding problem.

``````    Vector3 dir=transform.forward; // get our current direction
float angle=Vector3.SignedAngle(dir,player.position,transform.up); // get the angle of the player around our current up axis
dir=Quaternion.AngleAxis(angle,transform.up)*dir; // rotate our forward direction so that it points towards the player
transform.rotation=Quaternion.Lerp(transform.rotation,Quaternion.LookRotation(dir,transform.up),4*Time.deltaTime); // gradually look at the player.
``````

that got the enemy running in circles and stuff. the first code worked just fine, but only outside. once it gets inside it’s some how offset and missing the player. It seems as if it simply moves away, as if the direction vector is inverted, as shown in the gif.

I got the “pathfinding” figured out. I have a circle collider inside the planet, and register when the player is inside. Then the enemy will move to the contact point on the collider, and then chase the player again as it is now also inside.

Yeah it assumes you’ve already set the enemy’s up vector yourself using the surface normal. Whereas the previous method assumed the enemy was always outside on the planet’s surface and so would set the up vector accordingly.