Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SneezingCatP · Feb 04, 2012 at 02:22 AM · fpsaimcrosshairraycasts

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by SneezingCatP · Feb 04, 2012 at 09:03 AM

could you help me edit the machine gun script Dj

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

crosshair help needed 2 Answers

How make Raycast Aim to Crosshair? 1 Answer

Machine Gun Problems 1 Answer

Fps Controller fixed object (Gun) (Solved) 1 Answer

Crosshairs Gui don't stay the same how to fix??? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges