I have looked all over and can’t find anything that works for me. The closest I got to seperately rotating the base and barrel of a turret was something I found on here from a few years back.
But it makes them all jittery and they don’t look anywhere near their target. Just a mess.
And I just can’t figure it out
I have a turret, on a ship. It has a base and a barrel object. The script I found buried in here I converted from JS to C#:
using UnityEngine;
using System.Collections;
public class PSK_Turret : MonoBehaviour {
public Transform turretBase;
public Transform turretBarrel;
public float baseRotateSpeed;
public float barrelRotateSpeed;
public Transform target;
// Update is called once per frame
void Update () {
if (target != null) {
Quaternion tmpBaseRot = turretBase.localRotation;
Vector3 targetPointTurret = (target.position - turretBase.position); //get normalized vector toward target
Quaternion targetRotationTurret = Quaternion.LookRotation (targetPointTurret, turretBase.up); //get a rotation for the turret that looks toward the target
turretBase.rotation = Quaternion.Slerp(transform.rotation, targetRotationTurret, Time.deltaTime * baseRotateSpeed); //gradually turn towards the target at the specified turnSpeed
turretBase.localRotation = Quaternion.Euler( tmpBaseRot.eulerAngles.x, turretBase.localRotation.eulerAngles.y, tmpBaseRot.eulerAngles.z); //reset x and z rotations and only rotates the y on its local axis
Quaternion tmpBarrelRot = turretBarrel.localRotation; //preserves the local rotation since we only want to rotate on local x axis
Vector3 targetPointBarrels = (target.position - turretBarrel.position); //get a normalized vector towards the target
Quaternion targetRotationBarrels = Quaternion.LookRotation (targetPointBarrels, turretBarrel.up); //get a rotation that looks at the target
turretBarrel.rotation = Quaternion.Slerp(turretBarrel.rotation, targetRotationBarrels, Time.deltaTime * barrelRotateSpeed); //gradually turn towards the target as the specified turnSpeed
turretBarrel.localRotation = Quaternion.Euler( turretBarrel.localRotation.eulerAngles.x, tmpBarrelRot.eulerAngles.y, tmpBarrelRot.eulerAngles.z); //reset y and z rotations and only rotates the x on its local axis
}
}
}
Does not work though Does anybody have a good easy solution for aiming a turret properly at a target? I know I can use LookAt to make the whole thing rotate at the target… but that doesnt work right either. I did get a good base rotation with LookAt, but the turret itself had to be upright, if I rotated it sideways it would rotate itself back flat…
Any help would be greatly appreciated. Trying to figure this out has been killing me.