- Home /
SentryGun turret position is not working properly ! Need help
Hi !
I am working on Bootcamp demo ...i have tried to add sentrygun as an enemy in bootcamp demo ..so that when soldier comes in that range .
Sentrygun starts hitting on soldier.. It is working fine in that manner it hitting soldier but range is not accurate ..like soldier comes too closer to sentryGun and than its start hitting soldier..other than that one problem is there sentryGun Turret rotates towards soldier but first it points downwards like on soldier foot .which doesnot look good and efficient.
1 - Please tell me how to increase a range of sentryGun so it targets soldier from far away .
2- How to set sentryGun turret , so that it doesnt point soldier's feet .. it should point soldier's chest or head .
Here is an attached screen shot of soldier and sentryGun .
and here is script for sentryGun .
var attackRange = 30.0; var shootAngleDistance = 10.0; var target : Transform;
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) SendMessage("Fire");
}
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;
}
///////////////and here is a script of MachineGUn attached to sentryGun Turret/////// var range = 100.0; var fireRate = 0.05; var force = 10.0; var damage = 5.0; var bulletsPerClip = 40; var clips = 20; var reloadTime = 0.5; private var hitParticles : ParticleEmitter; var muzzleFlash : Renderer;
private var bulletsLeft : int = 0; private var nextFireTime = 0.0; private var m_LastFrameShot = -1;
function Start () { hitParticles = GetComponentInChildren(ParticleEmitter);
// We don't want to emit particles all the time, only when we hit something.
if (hitParticles)
hitParticles.emit = false;
bulletsLeft = bulletsPerClip;
}
function LateUpdate() { if (muzzleFlash) { // We shot this frame, enable the muzzle flash if (m_LastFrameShot == Time.frameCount) { muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward); muzzleFlash.enabled = true;
if (audio) { if (!audio.isPlaying) audio.Play(); audio.loop = true; } } else { // We didn't, disable the muzzle flash muzzleFlash.enabled = false; enabled = false;
// Play sound
if (audio)
{
audio.loop = false;
}
}
}
}
function Fire () { if (bulletsLeft == 0) return;
// If there is more than one bullet between the last and this frame // Reset the nextFireTime if (Time.time - fireRate > nextFireTime) nextFireTime = Time.time - Time.deltaTime;
// Keep firing until we used up the fire time while( nextFireTime < Time.time && bulletsLeft != 0) { FireOneShot(); nextFireTime += fireRate; }
}
function FireOneShot () { var direction = transform.TransformDirection(Vector3.forward); var hit : RaycastHit;
// Did we hit anything? if (Physics.Raycast (transform.position, direction, hit, range)) { // Apply a force to the rigidbody we hit if (hit.rigidbody) hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
bulletsLeft--;
// Register that we shot this frame, // so that the LateUpdate function enabled the muzzleflash renderer for one frame m_LastFrameShot = Time.frameCount; enabled = true;
// Reload gun in reload Time
if (bulletsLeft == 0) Reload();
}
function Reload () {
// Wait for reload time first - then add more bullets! yield WaitForSeconds(reloadTime);
// We have a clip left reload if (clips > 0) { clips--; bulletsLeft = bulletsPerClip; }
}
function GetBulletsLeft () { return bulletsLeft; }
Answer by scarletsnake · Apr 06, 2011 at 12:23 AM
1-I haven't gone through the entire code but from what i gather, you need to set the variable "attackrange" to a higher value, experiment with that. I'm pretty sure that's what you're looking for.
2-The easiest way to fix this is to create an empty object, place it within the soldier(make it a child of the soldier) place it whereever you want the turret to aim to, then set that object as the turret's target. Alternatively, you can code in the position you want the turret to aim, like var target : Transform //Assign the soldier as the target var targetpos =Vector3(target.position.x,target.position.y+desirednumber,target.position.z); //Set targetpos as the coordinates.
Hope this helps! ;)
Your answer
Follow this Question
Related Questions
Player health decrease and blood splatters ?? 2 Answers
Photon / Multiplayer - Bootcamp Demo 3 Answers
Can I use Bootcamp Map for commercial game? 1 Answer
Bootcamp particles 0 Answers
Bootcamp Photon Webplayer Not Working! 0 Answers