I’ve asked something similar 2 weeks ago but nobody answered.I’ve followed a youtube video for making a camera that rotates around an gameobject.The gameobject is a spaceship with turrets and i want the turrets to follow like the middle of the screen when i move the camera.I want it only follow on y axes so it’s rotates like a tank turret . This is the camera rotation script using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour
{
private const float Y_ANGLE_MIN = -35.0f;
private const float Y_ANGLE_MAX = 50.0f;
public Transform lookAt;
public Transform camTransform;
private Camera cam;
public float distance = 10.0f;
private float currentX = 0.0f;
private float currentY = 0.0f;
public float SensivityX = 60.0f;
public float SensivityY = 30.0f;
private void Start()
{
Cursor.visible = false;
camTransform = transform;
cam = Camera.main;
}
private void Update()
{
Cursor.visible = false;
currentX += Input.GetAxis ("Mouse X");
currentY += Input.GetAxis ("Mouse Y");
currentY = Mathf.Clamp (currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
}
private void LateUpdate()
{
Vector3 dir = new Vector3 (0, 0, -distance);
Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
camTransform.position = lookAt.position + rotation * dir;
camTransform.LookAt (lookAt.position);
}
}
so if you doesn’t understand i want somehow to link the turret rotation with the camera rotation so if i look back the turret rotates idk 180 degrees. I had a script that do that but it wasn’t linked to the camera so if i turned the SHIP to the right let’s say 50 degrees the turret rotates with the ship but the camera stays and disturb everything here is the script of the turret using
System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class turretTest : MonoBehaviour {
public float speed = 1f;
public float fireRate = 1f;
private float fireCountdown = 0f;
public GameObject LaserPrefab;
public Transform firePoint;
void Update ()
{
if (Input.GetAxis ("Mouse X") < 0 || (Input.GetAxis ("Mouse X") > 0))
{
transform.Rotate(0, (Input.GetAxis("Mouse X")), 0 * Time.deltaTime * speed );
}
if (Input.GetMouseButton(0))
{
if (fireCountdown <= 0f)
{
Shoot ();
fireCountdown = 1f / fireRate;
}
fireCountdown -= Time.deltaTime;
}
}
void Shoot ()
{
Instantiate (LaserPrefab, firePoint.position, firePoint.rotation);
}
}