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 chrisall76 · Jun 08, 2012 at 04:27 AM · raycastgunshooteffects

Raycasting to the middle of the screen?

I've found out how to make a gun shoot and thats great, but the code won't shoot directly in the middle of the screen (AKA crosshair). Any help? EDIT: I've noticed that the effect is played straight from the gun (right of the player). It's not taking in the rotation, so it won't shoot where i want too. So anyway to force it to take rotation/shoot to the middle of the screen?

Gun Script:

 var shotSound: AudioClip; // drag a shot sound here, if any
 var bloodPrefab: GameObject; // drag the blood prefab here, if any
 var sparksPrefab: GameObject; // drag the sparks prefab here, if any

 function Update(){
    if (Input.GetButtonDown("Fire1")){
       Shoot();
   }
 }

 function Shoot(){
 if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound
    var hit: RaycastHit;
 
 if (Physics.Raycast(transform.position, transform.forward, hit)){
     // prepare rotation to instantiate blood or sparks
     var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
 
     if (hit.transform.tag == "Enemy"){ // if enemy hit...
         if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot);
            hit.transform.SendMessage("ApplyDamage", 5,             
            SendMessageOptions.DontRequireReceiver); // and consume its health
 
        } else { // otherwise emit sparks at the hit point
           if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
     }
 }
Comment
Add comment · Show 3
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 kolban · Jun 08, 2012 at 04:26 AM 1
Share

What is this script attached to? What is the position of this game object? Does the position of this game object ever change?

When you say the code won't "shoot" directly in the middle of the screen, have you seen any "patterns"? Where does the code shoot at? How do you "know" it isn't shooting in the middle of the screen?

Always assume when posting questions that the reader doesn't know what you are describing so be as clear as possible, ideally hosting pictures or diagrams. The more time you spend creating a well written question, the more likely you are to get a well considered and knowledgable answer.

avatar image chrisall76 · Jun 08, 2012 at 04:42 AM 0
Share

Sorry, just edited it to give more information.

avatar image ObviouslyInsane · Jun 08, 2012 at 07:59 AM 1
Share

I assume you probably have this script attached to your gun which is positioned off to either side of your player. What you should do ins$$anonymous$$d is attach the script to an empty game object (or even the main camera if it FPS) and place it so the raycast is pointed straight out in front of the player.

2 Replies

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

Answer by Bunny83 · Jun 08, 2012 at 03:00 PM

It's easier to use Camera.ViewportPointToRay. The example on this page exactly shows what you want ;)

 // Unitycript
 var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
 
 if (Physics.Raycast (ray))
 {
     // ...
 }

Comment
Add comment · Show 1 · 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 asafsitner · Jun 08, 2012 at 03:51 PM 0
Share

Hmm. Truly so, truly so. $$anonymous$$ay as well provide more accurate results (Screen.width/height do calculate the entire screen resolution, whether windowed or not). +1 for better answer. :)

avatar image
2

Answer by asafsitner · Jun 08, 2012 at 01:58 PM

I believe using `Camera.ScreenPointToRay` will help.

Physics.Raycast(Camera.main.ScreenPointToRay(Screen.width / 2, Screen.height / 2, 0))

Should cast a ray directly to 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

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

7 People are following this question.

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

Related Questions

Making a shotgun? 1 Answer

FPS PISTOL 1 Answer

How do I make a gun project a particle? 1 Answer

How do I make a gun? 1 Answer

Using RayCast to do Continuous Shooting? 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