• 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 RPMonkey · Nov 19, 2014 at 10:26 PM · raycastgun

RayCast Shooting not working

Hi, I am new to unity this year, and have been having problems with the raycast shooting script. When I play my game, it spawns the effect, but it only spawns in the middle of map. I have had another game without terrain that the raycast worked perfectly. Is terrain the problem? ![alt text][1] Here is my script:

 #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.gameObject, 2);
             hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
         }
     }
     
 }
 
 
   [1]: /storage/temp/35514-raycast+shooting+not+working.png

raycast shooting not working.png (466.5 kB)
raycast shooting not working.png (466.5 kB)
Comment
Add comment · Show 9
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 SaraCecilia · Nov 20, 2014 at 02:14 PM 1
Share

Hey, how does your actual ray look when shooting in your terrain scene (using Debug.DrawRay)?

avatar image AndyAndreiH · Nov 20, 2014 at 03:05 PM 0
Share

Have you tried using Camera.ViewportToRay?

avatar image RPMonkey · Nov 21, 2014 at 12:20 AM 0
Share

Where do I put the Camera.ViewportToRay in? the gun or the camera? How do I use the Debug.DrawRay?

avatar image MrSoad · Nov 21, 2014 at 12:21 AM 0
Share

Put it on the camera as this is what you will be ai$$anonymous$$g with(the center of your current viewpoint). It is actually already on it, your are casting from the center of the $$anonymous$$ain Camera object.

For how to use the Debug.DrawRay :

http://docs.unity3d.com/ScriptReference/Debug.DrawRay.html

avatar image RPMonkey · Nov 21, 2014 at 01:36 AM 0
Share

So do I add my draw ray to my camera as well as my raycast shooting script? I have tried using the draw ray on the main camera in the first person controller, but nothing is drawn. The same particle system still spawns in the middle of map using the raycast shooting in the main camera as well.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Very simple picking up items script? 2 Answers

How to add colliders to Tron Like Trail 0 Answers

Raycast Destroys player. 1 Answer

I have a problem with my parkour C# script on final lines 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