- Home /
Machine Gun aim to crosshair
hey guys I need help with my machine gun. It uses raycast to fire. When i fire the impact is to the right of the crosshair i need help with making the impact always hit the crosshair
here is my machine gun script
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; var hitParticles : ParticleEmitter; var muzzleFlash : Renderer; var errorAngle = 5.0; var emptyHull : Rigidbody; var throwPos : Transform; public var Light1 : GameObject; public var Light2 : GameObject; public var Light3 : GameObject; public var ReloadAudio : AudioClip;
private var bulletsLeft : int = 0; private var nextFireTime = 0.0; private var m_LastFrameShot = -1;
var shot : AudioClip;
function Start () {
// We don't want to emit particles all the time, only when we hit something. if (hitParticles) hitParticles.emit = false; bulletsLeft = bulletsPerClip; }
function Damage (Damage1 : float) { damage = Damage1; }
function LateUpdate() { if (muzzleFlash) { // We shot this frame, enable the muzzle flash if (m_LastFrameShot == Time.frameCount) { muzzleFlash.transform.localRotation = Quaternion.AxisAngle(Vector3.forward, Random.value); muzzleFlash.enabled = true; Light1.active = true; Light2.active = true; Light3.active = true;
if (audio)
{
if (!audio.isPlaying)
audio.PlayOneShot(shot);
audio.loop = true;
}
}
// We didn't, disable the muzzle flash
else
{
muzzleFlash.enabled = false;
Light1.active = false;
Light2.active = false;
Light3.active = false;
enabled = false;
// Play sound
if (audio)
{
audio.loop = false;
}
}
}
}
function Fire () { GameObject.Find("Cartuccia 3D GOLD").animation.Play("ShellEject"); 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 () { if (emptyHull) { Instantiate(emptyHull.transform,throwPos.position,throwPos.rotation); } var direction = Quaternion.Euler(Random.insideUnitSphere*errorAngle) * transform.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);
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
// 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();
}
}
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 () { GameObject.Find("MachineGun").animation.Play("M4a1 Animation");
// Wait for reload time first - then add more bullets!
PlayReloadAudio();
yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (clips > 0)
{
clips--;
bulletsLeft = bulletsPerClip;
}
}
function GetBulletsLeft () { return bulletsLeft; }
function PlayReloadAudio(){
audio.PlayOneShot( ReloadAudio);
}
thanks Dj
Answer by ClydeCoulter · Feb 04, 2012 at 03:18 AM
Just use the camera's position and direction for your raycast, if your crosshair is in the middle of the screen.
Answer by SneezingCatP · Feb 21, 2012 at 08:45 AM
I fixed it using ScreenPointToRay
Here Is my script if anyone wants this
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; var hitParticles : ParticleEmitter; var muzzleFlash : Renderer; var errorAngle = 5.0; var emptyHull : Rigidbody; var throwPos : Transform; var Tracer : GameObject; public var Light1 : GameObject; public var Light2 : GameObject; public var Light3 : GameObject; public var ReloadAudio : AudioClip;
private var bulletsLeft : int = 0; private var nextFireTime = 0.0; private var m_LastFrameShot = -1;
var shot : AudioClip;
function Start () {
// We don't want to emit particles all the time, only when we hit something. if (hitParticles) hitParticles.emit = false; bulletsLeft = bulletsPerClip; }
function Damage (Damage1 : float) { damage = Damage1; }
function LateUpdate() { if (muzzleFlash) { // We shot this frame, enable the muzzle flash if (m_LastFrameShot == Time.frameCount) { muzzleFlash.transform.localRotation = Quaternion.AxisAngle(Vector3.forward, Random.value); muzzleFlash.enabled = true; Light1.active = true; Light2.active = true; Light3.active = true; Tracer.active = true;
if (audio)
{
if (!audio.isPlaying)
audio.PlayOneShot(shot);
audio.loop = true;
}
}
// We didn't, disable the muzzle flash
else
{
muzzleFlash.enabled = false;
Light1.active = false;
Light2.active = false;
Light3.active = false;
Tracer.active = 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 () { GameObject.Find("Cartuccia 3D GOLD").animation.Play("ShellEject"); if (emptyHull) { Instantiate(emptyHull.transform,throwPos.position,throwPos.rotation); } //var direction = Quaternion.Euler(Random.insideUnitSphere*errorAngle) * transform.forward; var ray = Camera.main.ScreenPointToRay (Vector3(Screen.width/2,Screen.height/2,0));//(Input.mousePosition); var hit : RaycastHit;
// Did we hit anything?
//if (Physics.Raycast (transform.position, direction, hit, range))
if (Physics.Raycast (ray, hit, 10000))
{
Debug.DrawLine (Vector3.zero, Vector3 (1, 0, 0), Color.red);
// Apply a force to the rigidbody we hit
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * ray.direction, hit.point);
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
// 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();
}
}
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 () { GameObject.Find("MachineGun").animation.Play("M4a1 Animation");
// Wait for reload time first - then add more bullets!
PlayReloadAudio();
yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (clips > 0)
{
clips--;
bulletsLeft = bulletsPerClip;
}
}
function GetBulletsLeft () { return bulletsLeft; }
function PlayReloadAudio(){
audio.PlayOneShot( ReloadAudio);
}
Your answer
Follow this Question
Related Questions
crosshair help needed 2 Answers
How make Raycast Aim to Crosshair? 1 Answer
Machine Gun Problems 1 Answer