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 kieron_extreme · Jun 26, 2013 at 08:25 PM · raycastgun

How do I create a raycast from the end of my gun to go wherever i have my gun pointed

I have a raycast script and i want to make it so that the ray gets casted from the end of my gun, so that when i click my mouse button the ray is created at the end of my gun and travel foward wherever my gun is pointed.

Here is my script: var endBarrel :Transform; var sound : AudioClip;

 var TheDamage = 100;
 var Effect : Transform;
 var Enemy : GameObject[];
 
 
 
 
  
 function Start() {
 
 
 
 }
 function Update(){
       var hit : RaycastHit;
      
       var ray =  Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
     if (Physics.Raycast (ray, hit, 100)) {
            hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
     }
      
      
 
 
      
      if(Input.GetButtonDown("Fire1")){
         // you can use PlayOneShot to specify the sound...
         audio.PlayOneShot(sound);
 
         audio.Play();
     }
 
       
        
         
          
           
             if (Input.GetMouseButtonDown(0)){
           if (Physics.Raycast (endBarrel.position, transform.forward, 100))
           {
  
       
           
           
           
           
           
           }
       }
    }
Comment
Add comment · Show 2
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 Graham-Dunnett ♦♦ · Jun 26, 2013 at 08:25 PM 0
Share

Line 39 looks like it does what you need. What problems are you having?

avatar image kieron_extreme · Jun 26, 2013 at 08:37 PM 0
Share

It casts the ray to the center of the screen i want it so that it comes out the end of my gun

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Em3rgency · Jun 26, 2013 at 08:49 PM

Try:

 var endBarrel :Transform; 
 var sound : AudioClip;
 var TheDamage = 100;
 var Effect : Transform;
 var Enemy : GameObject[];
  
 function Start()
 {
  
 }
 
 function Update()
 {
   var hit : RaycastHit;
  
   if(Input.GetButtonDown("Fire1"))
   {
     // you can use PlayOneShot to specify the sound...
     audio.PlayOneShot(sound);
  
     audio.Play();
   }
 
   if (Input.GetMouseButtonDown(0))
     {
       if (Physics.Raycast (endBarrel.position, transform.forward, hit, 100))
       {
         hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);    
       }
     }
   }
 }
Comment
Add comment · Show 11 · 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 kieron_extreme · Jun 26, 2013 at 08:55 PM 0
Share

Im confused could you give me an example

avatar image Em3rgency · Jun 26, 2013 at 09:00 PM 0
Share

EDIT: Wait, line 39 IS exactly what you need. If it casts from the center of the screen then what is your endBarrel doing at the center of your screen? $$anonymous$$ake sure its a weapon and in the correct position.

Line 17 is the one that should be co$$anonymous$$g out of the center of your screen... And thats where you're currently applying your damage. So just move the damage message past line 39 ins$$anonymous$$d.

avatar image kieron_extreme · Jun 26, 2013 at 09:11 PM 0
Share

No that doesnt work i know this is asking quite alot but could you edit the script so it is right you dont have to if you dont want to im just wondering

avatar image Em3rgency · Jun 26, 2013 at 09:18 PM 0
Share

I updated my answer.

avatar image kieron_extreme · Jun 26, 2013 at 09:24 PM 0
Share

No that still does not work would i need to tag my gun barrel or something

Show more comments
avatar image
0

Answer by brandonsbarber · Jun 26, 2013 at 08:46 PM

Check out this answer here. I think this should help. To calculate direction, just subtract vectors from both ends of the gun.

That is assuming your gun is an actual object in the scene...

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Using RayCast to do Continuous Shooting? 1 Answer

RayCast Shooting not working 0 Answers

Make bullet launch at center of screen 2 Answers

Joystick Raycast Problem? 2 Answers


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