I tried to create a empty gameobject and put the first gameobject as child but it didn't fix the problem. Just rotating the object a script is fine but if I'm using LookAt it's not rotating good since the gun is pointing on the red axis and the blue axis is on the body.
I tried simple LookAt :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAT : MonoBehaviour
{
private GameObject target;
// Use this for initialization
void Start()
{
target = GameObject.FindGameObjectWithTag("Enemy");
}
// Update is called once per frame
void Update()
{
transform.LookAt(target.transform);
}
}
Then I tried this : This was a bit better it seems like it was facing with the red axis forward with the gun but not perfect still it didn't target perfect the target I think not sure and not sure if this is a good solution :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAT : MonoBehaviour
{
public float speed;
private GameObject target;
// Use this for initialization
void Start()
{
target = GameObject.FindGameObjectWithTag("Enemy");
}
// Update is called once per frame
void Update()
{
Vector3 direction = target.transform.position - transform.position;
Quaternion toRotation = Quaternion.FromToRotation(transform.right, direction);
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed * Time.time);
}
}
The second solution I tried seems the red axis with the gun is facing the target now but the gun and the rings on the base are stuttering and the rings rotating too but they should not rotate I think or they should not be part of the looking on the target.
In this screenshot the whole rings on the base are kind of looking at the target and make the whole turret looks strange. So I'm not sure how ot make this rings and other stuff rotating while only the turret turn object will face the target.