Turret Control

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.

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

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

Any help would be greatly appreciated.

Script SmoothLookAt.js that comes with Unity makes an object targets another one. Attach it to your game object (number 1 in your list). You´ll just need another one to fire your bullets towards the target.

Hi, thanks for posting,

I’m not sure you understood the problem. 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.

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

If I have misunderstood what you mean, can you please clarify what you think I should do.

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 sctipt but once again, I’m not sure how.

Can you post the code you’ve got so far?

Are you controlling the turrets using an incremental control (such as left/right buttons), or click on a location and the turrets should target the locations (like missile command)?

Think fps with the mouse controling the rotation of the turrets and elevation of the guns.

Will post all current working code when I get home

here is what I have working at the moment.
Improvements needed include:

  • gun recoil
  • no “z” rotation
  • link to other guns in this battery
// contril 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;

// max/min angles
var mxX : float = 55.0;
var mnX : float = -55.0;
var mxY : float = 55.0;
var mnY : float = -55.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;
var gunspeed : float = 15;
var guntime = 0;


function Update () 
{
	xDeg -= Input.GetAxis("Mouse X") * speed * friction;
	yDeg += Input.GetAxis("Mouse Y") * speed * friction;
	
	//test angle min/max
	if (xDeg >= mxX)
	{
		xDeg = mxX;
	}
	if (xDeg <= mnX)
	{
		xDeg = mnX;
	}
	if (yDeg >= mxY)
	{
		yDeg = mxY;
	}
	if (yDeg <= mnY)
	{
		yDeg = mnY;
	}


	fromRotation = transform.rotation;
	toRotation = Quaternion.Euler(-yDeg,-xDeg,0);
	transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime  * lerpSpeed);
	
//control gun speed
	guntime = guntime - 1;
	
//fire guns
	if (Input.GetButton("Fire1"))
	{
		if (guntime <= 0)
		{
			var bullit1 = Instantiate(bulletPrefab,transform.Find("Spawn Point1").transform.position, transform.rotation);
			bullit1.rigidbody.AddForce(transform.forward * 2000);
			guntime = gunspeed;
		}
		
		if (guntime == Mathf.Ceil ((gunspeed * 0.5)))
		{
			var bullit2 = Instantiate(bulletPrefab,transform.Find("Spawn Point2").transform.position, transform.rotation);
			bullit2.rigidbody.AddForce(transform.forward * 2000);
		}
	}
}

The easiest way to separate elevation and azimuth for a gun is to use a parent object as a “mount” for the gun. Basically, you rotate the parent only around its Y axis and elevate the gun around its Z axis. This way you intrinsically keep the two rotations separate and don’t have to worry about composing them into one.

Thanks for that,

I have managed to create a script to link the battery to the controlling turret, so now all I need is the code to make the gun recoil and return to position when fired.

updating the following code can anyone tell me how to limit the rotation of the turret but remember the rotation generated by the mouse?

This update will limit the rotation of turrets in a battery so that they cannot shoot each other but will still be in alignment when we turn our main turret so that it is within the limits of the others.

eg.

control turret can turn from 100 to -100 degrees

turret one can only be turned from -100 to 0 degrees
turret two can only be turned from 100 to 0 degrees

//comand set
var turretGroup : int = 0;
var sitAt : int = 0;

// control vars
var speed : int = 3;
var friction : float = 1.0;
var lerpSpeed : float = 1.5;
var xfix : int = 0;

//angles
var xDeg : float = 0.0;

// max/min angles
var mxX : float = 55.0;
var mnX : float = -55.0;


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

function seat (seat : float) 
{
    sitAt = seat;
}

function Update () 
{
	if (sitAt == turretGroup)
	{
		if (turretGroup == 1)
		{
			xDeg -= Input.GetAxis("Mouse X") * speed * friction;
			
			//test angle min/max
			if (xDeg >= mxX)
			{
				xDeg = mxX;
			}
			if (xDeg <= mnX)
			{
				xDeg = mnX;
			}

	
	
			fromRotation = transform.rotation;
			toRotation = Quaternion.Euler(xfix,-xDeg,0);
			transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime  * lerpSpeed);
			
		}
	}
}