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 Agent000 · Mar 08, 2015 at 12:04 PM · raycastfpsaiming

FPS aim is slightly off center - using ScreenPointToRay

I am making an fps game but my shots always end up slightly below and to the right of where they are supposed to hit (whatever is in the center of the screen.) I've used the ScreenPointToRay function to aim the gun at whatever point is currently displayed in the center of the screen. Problem is, when I actually fire the gun the projectile ends up the slightest bit off of where it should be. See here:

alt text

That is where the shot hits the wall (I've tested enough to know it's not just me moving the reticle after firing.)

I've tried everything I can think of -- I used a debug raycast to make sure the shot spawner on the end of the gun is rotated to face the target point; I've double checked that the raycast points toward the center -- the point at which the gun should be aiming is definitely correct. Could it be possible the bullet object itself is offset from its origin somehow? It doesn't look like it in the inspector but I really don't know. Please help, I'm running low on ideas!

pointoff.png (13.3 kB)
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 meat5000 ♦ · Mar 07, 2015 at 12:02 AM 0
Share

Does the error reduce over a larger distance?

Are you taking in to account the difference in position between the centre of your camera and the gun?

avatar image Agent000 · Mar 07, 2015 at 03:23 PM 0
Share

Yes, the error does reduce over a larger distance. So I might not be taking into account the difference in position, but I use a LookAt function to point the bullet spawn object at the hit point of the ScreenPointToRay, so I would think that would make it always face exactly at that point.

avatar image Agent000 · Mar 09, 2015 at 12:38 PM 0
Share

I realized it's possible the error is caused by the bullet spawn point (which is invisible) not actually rotating to face the aim point, resulting in the gun just firing straight forward. But I'm not sure how that would occur unless I'm somehow using the LookAt function incorrectly.

2 Replies

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

Answer by Agent000 · Mar 09, 2015 at 09:57 PM

Haha, it was something stupid I missed. Knew it had to be something like this. My script was referencing the wrong object's rotation -- I had it attached to the gun object so it was referencing the gun's rotation not the BulletSpawn's rotation. Fixed, working fine now. Thanks for the help :)

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 Enemby · Mar 09, 2015 at 02:22 PM

First off, why are you spawning the bullet spawn point from the actual gun? I think that is probably the cause of your problem, since gun isn't centered on the camera's center (transform wise, that is)

You don't need to spawn the bullet at the gun's location, especially in the case of a raycast weapon.

Comment
Add comment · Show 2 · 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 meat5000 ♦ · Mar 09, 2015 at 02:59 PM 0
Share

If the bullet is a raycast and not a gameobject this approach may make sense. However when you have a guy running round with bullets co$$anonymous$$g out of his face, it doesn't really hold up. Its best to calculate the difference. The latter is the approach they use in most games. You can see this when you fire at a wall or something at different distances. Stand right up against an object and shoot and you can see which method they've used.

avatar image Agent000 · Mar 09, 2015 at 07:09 PM 0
Share

What would I spawn them from if not the gun? I'm not literally spawning the bullets from the actual gun model, I have an invisible gameobject BulletSpawn childed to the end of the gun model, and this BulletSpawn object uses a LookAt function to face the raycast hit point. Then the bullet is supposed to spawn with the same rotation and position as the BulletSpawn object, and move in its forward direction. If either the LookAt function or setting the bullet's transform isn't working for whatever reason, that would cause the bullet to just fire straight out of the gun, which could cause the problem. I'm going to check whether the BulletSpawn object is actually rotating like it's supposed to -- I'll get back to you once I've done 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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why does this script I got from the Unity Document site does not work? 1 Answer

Hit distance Change light range 1 Answer

Aiming Down Sights Problem 2 Answers

Shooting script no working correctly 1 Answer

Raycast 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