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 niceperson87 · Apr 11, 2011 at 09:03 PM · fpsshootaimguns

Aim to crosshair?

how do i make it so the bullet always goes straight towards the middle of the crosshair even if the bullet isnt at the middle of the screen?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Meater6 · Apr 11, 2011 at 11:33 PM

Ok... If you have a gun, say like on the side of the screen like in a FPS, you realize that even though it shoots strait into space and the bullet ends up at the center of the screen, it doesn't work when the thing you are shooting is closer. I think this is your question, but I am not sure.

To solve this, try making a raycast from the center of the screen (or where your crosshair is) to the 3d environment. Make it very long, but not infinite. Then make the spawning object at the tip of the gun and make it always face where the raycast hits a object. The reason for making the raycast really long is because if it doesn't hit a object, you must make the spawner always shoot to the endpoint of the ray, giving the effect of shooting directly in the center of the screen. I have not tested this method, but in theory, it works.


EDIT:

If you don't know how to cast a ray, and gain information from the hit, look at the unity scripting reference on raycasting here. If you know how to do that and just don't know how to cast from the center of the screen, use this:

var ray : Ray = camera.ScreenPointToRay (Vector3(512,384,0));

if(Physics.Raycast(ray,hit,100000.0)){ //set the spawn object to aim here }

That Vector3(512,384,0) is the screen coordinates for the center of a Standalone(1024,768) screen only, you will have to change those values if the game screen is not that size. Also, if your gun shoots stuff that are invisible, like bullets, you don't need a spawner, or even a gun. Just make a raycast from the center of the screen when you click. If it hits a enemy, make the enemy loose health, for example. The large ray length is for the "shoot into space" effect, when your ray hits nothing, and you still shoot, then set the spawner to aim at the endpoint.

I hope this answers your question.

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 niceperson87 · Apr 12, 2011 at 11:16 PM 0
Share

Thanks! but how do i do this?

avatar image
0

Answer by NewfieJoe · Apr 12, 2011 at 04:21 AM

You could do a raycast first from camera straight forward then adjust the gun aim to point at the raycast hit with a default to a certain range if there is no hit.

Comment
Add comment · Show 6 · 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 Meater6 · Apr 12, 2011 at 04:48 PM 0
Share

repeating answer, of what I said above^. But maybe it is more clear and helpful.

avatar image NewfieJoe · Apr 12, 2011 at 05:39 PM 0
Share

I didn't refresh the page before posting. Yours wasn't there at the time for me. :)

avatar image niceperson87 · Apr 12, 2011 at 11:16 PM 0
Share

thank you! but i need to know how to use a raycast :p

avatar image NewfieJoe · Apr 13, 2011 at 02:56 AM 0
Share

http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html?from=RaycastHit

avatar image Meater6 · Apr 13, 2011 at 11:40 PM 0
Share

I thought of something, what if you move right next to a wall? Your gun might be parallel to the wall, to compensate for the close range. That might look awkward.

Show more comments
avatar image
0

Answer by jamie1 · Nov 17, 2011 at 07:05 AM

hey im have a similar problem, well i have my gun and the spwan point for the bullit right in front of the nozzle (tip) and it shoots fine except if you watch the bullit travel it flys and then is almost pulled to the floor almost as if the bullet is magnetically attracted to the floor, so if i aim up woods the bullet flys straight for the ground. ANy thoughts ???

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 syclamoth · Nov 17, 2011 at 07:06 AM 0
Share

Please post a new question, ins$$anonymous$$d of posting useless copypasta 'answers' to different questions.

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

2 People are following this question.

avatar image avatar image

Related Questions

Aiming Gun at Cursor 2 Answers

Aim and than shoot 2 Answers

Yield WaitForSeconds only works sometimes?! 1 Answer

FPS rigidbody bullets not moving to center of screen 0 Answers

Spawn point aim at mouse to shoot 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