Turret control Problem

Ok, here we go.

I am trying to create a turret system in which you have control of a set of turrets at the same time. The turrets will turn in unison with same rotation and gun elevation.

These turrets could be considered to be equivalent to AA batteries on a battleship but with only one operator.

The turrets aren't meant to be AI controlled, they are meant to be player controlled and rotate in synchronization. The objective is not accuracy, but for the player to be able to spray fire in a general direction.

The guns will also fire at the same time.

(if you don't understand, check out any ship in battlefield1942)

I think the hierarchy should be

0 star cruiser 1 game object

2 all add on turrets + control turret

3 camera (sub to control turret)

Not sure on the scripting but have tried to use mouselook and failed

I have been attempting to create my own .js from scratch but cannot stop the first turret (haven't even tried to link them yet) from rotating around the z axis.

I am aware that I need to tell it to rotate in global, not local, space but as of yet I have had no luck.

My plan was to have a control script on the turret with the camera, a variables script on the parent and a follow script on all the other turrets that obtains the rotation and fire commands from the parent.

You can probably do it without the variables script but once again, I'm not sure how.

Any help would be greatly appreciated!!

There are definitely a million different ways you could control multiple turrets in sync. The following is a C# example that might point you in the right direction. This would be attached to the player's ship. Each turret would have a "Turrets" tag assigned to it and would be a child of the ship.

using UnityEngine;
using System.Collections;

public class TurretControl : MonoBehaviour
public int rotMult = 30;

private GameObject[] turrets;
private float xRot = 0;
private float yRot = 0;

void Start()
    turrets = GameObject.FindGameObjectsWithTag("Turrets");
void Update()
    xRot += Input.GetAxis("Vertical") * rotMult * Time.deltaTime;
    yRot += Input.GetAxis("Horizontal") * rotMult * Time.deltaTime;

    for (int i = 0; i < turrets.Length; i++)
        turrets*.transform.rotation = Quaternion.Euler(new Vector3(xRot, yRot, 0));*

Not sure from you post exactly what is or isn't working, but this should get you started on rotating the Turrets

And maybe you could use BroadcastMessage to tell the other turrets how to rotate.

Also look at the FPS tutorial as it has some turrets that follow the player and attack. Lots of good code to start from in that Tutorial


// control vars

var speed : int = 3; var friction : float = 1.0; var lerpSpeed : float = 1.5;

//angles private var xDeg : float = 0.0; private var yDeg : float = 0.0;

//no idea but it works - part of calc we didn't cover at westland high private var fromRotation : Quaternion; private var toRotation : Quaternion;

//shooting var bulletPrefab : Transform;

function Update () { xDeg -= Input.GetAxis("Mouse X") * speed * friction; yDeg += Input.GetAxis("Mouse Y") * speed * friction; fromRotation = transform.rotation; toRotation = Quaternion.Euler(yDeg,xDeg,0); transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed);

if (Input.GetButtonDown("Fire1"))
    var bullit = Instantiate(bulletPrefab,transform.Find("Spawn Point").transform.position, Quaternion.identity);
    bullit.rigidbody.AddForce(transform.forward * 2000);



Here is what I meant to post ( copied the wrong code before)

Still cannot get the z axis to stay at a fixed rotation of ZERO.


use the same controlling script for all turrets - the input is the same so the angles will be the same provided they all have the same rotation to start with (also no fancy business with sending messages)

attach the camera to one

and it works