Hello. I’m trying to create a tank, with a turret that will rotate 360. I want the gun and turret to point towards whatever collider is at the center of the camera.
using UnityEngine;
using System.Collections;
public class TurretRotation : MonoBehaviour {
public Camera PlayerCamera;
void Update () {
RaycastHit hit;
Ray ray = Camera.main.ViewportPointToRay (new Vector3 (0.5f, 0.5f, 0f));
if (Physics.Raycast (ray, out hit)) {
transform.LookAt(hit.point);
}
}
}
If it is offset by 90 degrees, your model’s forward axis is offset by exactly those 90 degrees. You may directly resolve that within the game object or as mentioned by Ironmax, multiply the rotation by a 90 degrees rotation, though it should look like that in my opinion (WARNING UNTESTED CODE):
What this code does is to calculate the look at rotation “manually” and then multiply it with the required offset.
You can certainly compact it into one line. The offset might be wrong, as it can be +/-90 degrees in any direction. You need to find that out on your own.