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 Binxalot · Feb 27, 2015 at 04:54 PM · raycastingmouseposition

Offsetting a Mouse Point Raycast?

Hi,

I have a math problem I'm just not able to figure out and I was wondering if anyone can suggest a solution.

I have a FPS/Rails shooter game. The bullets are physical mesh models that get fired from the gun. The gun is a gameobject with no mesh and the bullets fire from the guns position.

The crosshair follows the mouse pointer's position and so the ray gets cast from the camera's position to the mouse point and the raycast's hit point is the point used for aiming the gun's bullets.

What I would like is to be able to move the gun's position slightly to the right or left of the camera so bullets can be seen better. The problem is when I do this the aim gets skewed because while the bullets will fire from the gun's position, the hit point is calculated from the camera's position and the mouse position. This causes the bullets to fire offcenter of the crosshair. This also visually confusing.

So what I'm trying to figure out is how I would fire the bullet from the gun and have the hit point still appear as being centered in the crosshair when the gun is offset from the camera's position. I made a little illustration to try and show what I'm talking about below.

Also to clarify. By visually interesting / uninteresting, I'm talking about hit this appears to the player while playing the game. When the gun is offset to the left or right the bullets can be seen by the player, but when its centered to the cameras position the bullets always come from the middle of the screen which looks odd.

camera offset example

camexample.png (17.4 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by maccabbe · Feb 27, 2015 at 06:26 PM

Use the following code. It gets the position you want to hit and the position you want the bullet to start then gets the direction the bullet should travel using the difference between the two.

 void Update() {
     if(Input.GetKeyDown(KeyCode.Mouse0)) {
         Ray ray=camera.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit)) {
             Vector3 target=hit.point;
             Vector2 offsetMouse=new Vector2(Input.mousePosition.x+x, Input.mousePosition.y+y);
             Vector3 start=camera.ScreenToWorldPoint(offsetMouse);
             Vector3 direction=(target-start).normalized;
             CreateBullet(start, direction);
         }
     }
 }
 void CreateBullet(Vector3 startPoint, Vector3 direction) { 
     // make a bullet at start point that flies in direction
 }
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

19 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 avatar image

Related Questions

How to Limit Input.mousePosition or Raycast 1 Answer

Ray Over Mouse 1 Answer

Can someone explain why my Raycasting doesn't work? please :) 2 Answers

How to find out which object is under a specific pixel 1 Answer

Rotate Object with mouse in RTS style game 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