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 JMgames · Nov 11, 2014 at 10:41 PM · raycastinggun

Raycast off empty gameobject rather then camera.

I want to cast a ray(8 actually) from my airplanes gun(s). The problem is my script is designed to shoot the ray from the center of the camera. I need a way to project the ray forward from my gun(s).

 #pragma strict
 
 var Effect : Transform;
 var TheDammage = 100;
 
 function Update () {
 
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
     
     
     if(Input.GetMouseButtonDown(0)){
         if(Physics.Raycast (ray, hit, 100))
             {
                 var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
                 
                 Destroy (particleClone, 3);
                 
                 hit.transform.SendMessage("ApplyDamage", TheDammage, SendMessageOptions.DontRequireReceiver);
             }
             
     }
 
 }
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

2 Replies

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

Answer by MrSoad · Nov 15, 2014 at 11:11 PM

This line should help you, it casts from the object that it is on, in the objects forward direction, with "fRaycast_Distance" distance and "lmPlayer" layermask.

 if (Physics.Raycast (transform.position, transform.forward, hit, fRaycast_Distance, lmPlayer)) {

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 Owen-Reynolds · Nov 12, 2014 at 03:51 AM

That code is using a specific shortcut to look from the camera to a specific point on the screen. But, in general, you can raycast "in the world" from any 3D point, in any 3D direction.

Looking at other examples of raycasts should let you see how it's done. For example, a player shooting a bullet is sometimes a raycast from the gun, in the direction it faces.

Comment
Add comment · Show 3 · 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 JMgames · Nov 15, 2014 at 10:50 PM 0
Share

I was hoping for an example rather then a general answer.

avatar image Jeff-Kesselman · Nov 15, 2014 at 11:15 PM 0
Share

http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

http://bit.ly/1sUBuv6

avatar image Owen-Reynolds · Nov 16, 2014 at 01:32 AM 0
Share

Unity Answers is full of perfectly good examples of raycasts, esp. for shooting. Code snippets like you posted, with people explaining and making it work. Very popular subject.

Nothing I quickly type here is going to be as good as all that.

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

Using CrossHair GUI to Determine Ray Spread 0 Answers

Raycast shooting 1 Answer

Gun script not working 3 Answers

Raycast shooting in the middle of the screen 1 Answer

How can I shooting script c#? 0 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