Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by dowak23 · Jan 09, 2016 at 12:22 PM · c#gungun script

Inaccurate shooting script for gun

Hello guys. I was making my own script to let my gun shoot.

I've tried a lot of ways, from raycasting to instantiating my bullet game object, and the last option is working near to perfectly.

The problem is, the script is inaccurate, becouse when my bullet spawn and goes foward, it goes under my crosshair.

Is there a wait to make it 'right'? I mean, make my gun shoots like in other fps games?

Here are the scripts : http://pastebin.com/UX89xJmJ and http://pastebin.com/pais1Ur0 Here are screenshots : http://i67.tinypic.com/a9wewp.jpg and http://i64.tinypic.com/334uuxw.png and the last one http://i64.tinypic.com/zlqjj5.jpg

Maybe that will help you better understand my problem :)

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
Best Answer

Answer by unnamed1334 · Jan 09, 2016 at 06:56 PM

The bullet are likely inaccurate because you are creating them to the side of the camera but not adjusting the bullets to make sure they hit where the camera is looking. You can use a raycast to get the point the camera is looking at and the adjust the bullet so it travels towards that point.

 RaycastHit hit; // the output of the raycast
             
 //check to see if the camera is looking at anything, if it is get the exact point
 if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit)) {
     //rotates the bullet so it is pointing at the exact point the player is looking at
     shooted.transform.LookAt(hit.point);
 }
 

The bullet will still end up falling below the crosshair because of gravity but this, at least makes, the bullet initially accurate. You could also disable gravity for the bullets if you don't want them to fall as they travel.

This wasn't asked in the question but two other things that might cause problems in the future:

You are using Time.deltaTime instead of Time.fixedDeltaTime inside FixedUpdate so the cool down on firing might be much longer then you expect.

Instead of using AddForce, you might want to set the velocity of the bullet directly so you can know how fast the bullet moves.

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 dowak23 · Jan 09, 2016 at 08:01 PM 0
Share

Thank you very much for your help :D Now it is working properly :) And yea, I've forgot about chaning Time.deltaTime to Time.fixedDeltaTime before posting this :) Any way, thanks a lot for it :)

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

56 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 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 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

AddForce not working on Instantiated prefab 0 Answers

Damage Dropoff 1 Answer

Ammo changing between guns 0 Answers

Unity 2d simple c# shooting script 1 Answer

I've been trying to make a gun script, but somehow the boolean to tell when the bullet is destroyed doesent work 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