Aiming to gameobject

Hi, as the title says I’m trying to make my character aim towards an object, but the rotation is slightly off, picture for reference.

In theory it should aim at the centre of the gameobject.
Here is my code:

if (Input.GetMouseButtonDown(1))

			Ray ray = cam.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;

			if (Physics.Raycast(ray, out hit, 100))
				InteractableEnemy interactable = hit.collider.GetComponent<InteractableEnemy>();

				if (interactable != null)

void JustSetFocus(InteractableEnemy newFocus)
		if (enemy != null)
		enemy = newFocus;
		Vector3 direction = enemy.gameObject.transform.position - transform.position;
		Quaternion rotation = Quaternion.LookRotation(direction);
		transform.rotation = rotation;


I must add that it only aims at the centre of the gameobject when there is a significant distance between them, but not when close as in the picture.

Any tips are appreciated, thank you.

When you calculate the Vector3 direction, don’t find the difference of the two positions, do something like this, where the only thing that is affected is the y rotation

Vector3 DIrection = (new Vector3(enemy.transform.position.x, player.position.y, enemy.transform.position.z)) - player.position);

This calculates direction, but with respect only to forwards and backwards.
Hope this helps!

I suggest you use Transform.LookAt() instead. Look it up on Unity Docs.