How can i rotate both turret cannon and body same time or separate ?

I have in the Hierarchy a Turret and as a child also a Cannon. of the Turret. Then i have a Cube i’m using for testing to see if and when the turret detecting it and rotating facing the cube.

The problem is that if i put the Cannon as child of the Turret and the script is on the Turret it will rotate with lag. But if i put the script only on the Cannon then the Cannon will rotate fine facing the Cube but the Turret body will not rotate.

I want to rotate both Turret body and Cannon together.

I recorded a small video showing the problem. First when the script is attached to the Cannon and then when the script is attached to the Turret.

And this is the script i’m using for rotating the turret: Either using the code it is now in Update or using the LookAt line same problem.

using UnityEngine;
using System.Collections;

public class Turret : MonoBehaviour
{
    public GameObject target;
    public float smooth = 1f;
    public float rangeSqr = 30f;

    private void Update()
    {
        float distanceSqr = (transform.position - target.transform.position).sqrMagnitude;
        if (distanceSqr < rangeSqr)
        {
            //transform.LookAt(target.transform.position);

            var targetRotation = Quaternion.LookRotation(target.transform.position - transform.position);
            var str = Mathf.Min(70f * Time.deltaTime, 1);
            transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str);
        }
    }
}

I also tried to change the Pivot/Center option in the editor but nothing helped so far.

It looks like in the 2nd part (where script is on the turret), the turret IS looking at the cube, but the cannon (the red thingy) is not on the correct side. In other words, you should put the cannon on the correct side of the turret ball; the side of the turret that’s looking towards the player. At 1:15, you can see you’ve got the cannon on the red (X) axis arrow. Try putting it on the blue (Z) axis arrow.

If this can help you :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TurretController : MonoBehaviour {

    [Header("Attributes")]
    public Transform partToLook;
    private Transform target;
    public Transform bulletPlace;
    public GameObject bulletPrefab;

    [Header("Unity Setup Field")]
    public float fireRate = 1f;
    private float fireCountDown = 0f;
    public float range = 15f;
    private float turnSpeed = 7.0f;
    public float bulletSpeed = 19f;

    [Header("Tags")]
    public string enemyTag = "Enemy";

    // Use this for initialization
    void Start () {
        InvokeRepeating ("UpdateTarget", 0.0f, 0.5f);
    }



    void UpdateTarget (){

        GameObject[] enemies = GameObject.FindGameObjectsWithTag (enemyTag);
        float shortestDistance = Mathf.Infinity;
        GameObject nearestEnemy = null;

        foreach (GameObject enemy in enemies) {
            float distanceToEnemy = Vector3.Distance (transform.position, enemy.transform.position);
            if (distanceToEnemy < shortestDistance) {
                shortestDistance = distanceToEnemy;
                nearestEnemy = enemy;
            }
   
            if (nearestEnemy != null && shortestDistance <= range) {
                target = nearestEnemy.transform;
            } else {
                target = null;
            }
        }
   
    }


    // Update is called once per frame
    void Update () {

        if (target == null)
            return;
       
        Vector3 dir = target.position - transform.position;
        Quaternion lookRotation = Quaternion.LookRotation (dir);
        Vector3 rotation = Quaternion.Lerp(partToLook.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles;
        partToLook.rotation = Quaternion.Euler(0.0f, rotation.y, 0.0f);


        if (fireCountDown <= 0f) {
            Shoot ();
            fireCountDown = 1f / fireRate;
        }

        fireCountDown -= Time.deltaTime;

    }


    void OnDrawGizmosSelected (){
        Gizmos.color = Color.green;
        Gizmos.DrawWireSphere (transform.position, range);
    }

    void Shoot(){
        GameObject bulletGO;
        bulletGO = (GameObject)Instantiate(bulletPrefab, bulletPlace.position, bulletPlace.rotation);
        Bullet bullet = bulletGO.GetComponent<Bullet> ();

        if (bullet != null) {
            bullet.Seek (target);
        }
    }


}

The rotation in this script will work but where is the Bullet component(Script) ?

Bullet bullet = bulletGO.GetComponent<Bullet> ();

I don’t have Bullet.

If i change the Cannon the red thing rotation to 0,0,0 the it will rotate facing the target fine but it also looks like not connected to the blue sphere(the body of the turret):

And i want it to looks like that but then it will not rotating fine:

Well, after rotating the red thing’s rotation, you can move it wherever you want it to be… its position has nothing to do with its rotation.

1 Like