I am just starting out with unity, and 3d game development in general. I have gone through the Unity Game Development essentials guide, so I have a basic understanding of the software and scripting languages.
I created a turret with Maya that has two gatling barrels on it. I want the turret to be able to rotate left right and up down by follow targets. I also want the barrels to spin at the same time.
My first thought was that I could just do a ten second animation of the barrels spinning, and go from there. That works except as soon as you move the turret the animation keeps the barrels in the same position they were when you recorded them.
Ok, so that didn't work my next thought was that I could script the barrels to rotate. This worked to but as soon as the turret was rotated the barrels would then rotate about a different axis.
Then I decided that somebody had to have already done this so I searched through this site and the rest of the internet. I found several example scripts, and post explaining the many different ways to make turrets work. I've tried a couple of different scripts and gotten the main turret to rotate, and fire. But the firing positions did not rotate with the turret so the projectiles came out of the barrels at a ninety degree angle.
So what I am trying to accomplish is that I want the turret to rotate, and at the same time I want the barrels to rotate along their own axis. Each time one of the barrels reaches the top position of the barrels rotation then I want it to fire. In my imported model I have already added empty game objects to the barrels one for the firing position and one to monitor the barrels position.
Rotation script
var attackRange = 30.0;
var shootAngleDistance = 10.0;
var target : Transform;
static var Fire : boolean = false;
function Start () {
if (target == null && GameObject.FindWithTag("Player"))
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
if (target == null)
return;
if (!CanSeeTarget ())
return;
// Rotate towards target
var targetPoint = target.position;
var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
// If we are almost rotated towards target - fire one clip of ammo
var forward = transform.TransformDirection(Vector3.forward);
var targetDir = target.position - transform.position;
if (Vector3.Angle(forward, targetDir) < shootAngleDistance){
Fire = true;}}
function CanSeeTarget () : boolean
{
if (Vector3.Distance(transform.position, target.position) > attackRange)
return false;
var hit : RaycastHit;
if (Physics.Linecast (transform.position, target.position, hit))
return hit.transform == target;
return false;
}
Firing script
var throwSound : AudioClip;
var projectileObject : Rigidbody;
var throwForce : float;
static var Fire : boolean = false;
function Update () {
if(TurretAi.Fire == true){
Fire = true;
}
if(Fire == true) {
audio.PlayOneShot(throwSound);
var newProjectile : Rigidbody = Instantiate(projectileObject,
transform.position, transform.rotation);
newProjectile .name = "Rock";
newProjectile .rigidbody.velocity = transform.TransformDirection
(Vector3(0,0, throwForce));
/*Physics.IgnoreCollision(transform.root.collider,
newCoconut.collider, true);*/
}
}