• 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 SohaibTheGame · Jul 07, 2013 at 12:23 PM · raycastfpsmousepositionshootray

Raycast hitting below mouse position :(

Hello Unity people, i'm making a third person shooter and I'm facing some difficulties figuring out the best way for aiming the gun.

What i did was creating a camera in the hole of the gun that casts a ray to the mouse position, i used Debug.DrawLine to see where the ray is going, and it always goes below the mouse.

This is my script: #pragma strict var rayOrigin : Camera; function Start () {

 }
 
 function Update () {
 var ray = rayOrigin.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit, Mathf.Infinity)) {
         Debug.DrawLine (ray.origin, hit.point, Color.red);
         Debug.LogError("you hit something");
     }
 }

i was pointing at the center of the cube in my game view, but the ray was pointing below it, like in this picture

alt text

Help me please

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 robertbu · Jul 07, 2013 at 01:55 PM 0
Share

Is what you want here for the ray to go from the gun to the position under the mouse cursor? What do you want to happen if the cursor is behind the person? Below the person? What happens if the cursor is not over an object?

avatar image SohaibTheGame · Jul 07, 2013 at 05:17 PM 0
Share

After reading your comment, i realized that my way of making the gun shoot is wrong, i'm going to look for other examples to figure out a better way, thank you

avatar image robertbu · Jul 07, 2013 at 05:25 PM 0
Share

It's not wrong, but you have to consider all the situations and what you want to happen. And I needed to understand what you wanted to happen to give you an answer.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

3rd person 3D aiming 1 Answer

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

FPS PISTOL 1 Answer

How can i tell the distance from a raycast? 1 Answer

RayCasting shows wrong point 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