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 /
avatar image
0
Question by PanzerPotato · May 27, 2018 at 04:49 PM · scripting problemaimingmultiple objectsgun scriptship

Multi-gun Aiming System

Hi guys,

I'm still moderately new to Unity, and this is the first time that I am going to be working with ships. On these ships, there are various weapons on various parts of the ship. I am using a Raycast from the camera to determine where the guns are to aim. However, the guns don't seem to be aiming at the same spot... alt text

If you would like to see more info (scripts, how things work, etc.) please comment below and I will update this topic.

Please help!

(ps. I am trying to replicate War Thunder's aiming system for ships, with a little bit of inaccuracy in the aiming of each gun, so if you could offer some expertise it would be greatly appreciated :D )

(ps. again: I also use C#, but I can convert from javascript)

screenshot.png (185.0 kB)
Comment
Add comment · Show 2
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 ImpOfThePerverse · May 27, 2018 at 06:58 PM 0
Share

I was doing something like this for an FPS where you could potentially have a weapon in either hand, a head-mounted spotlight, and shoulder cannons, and was using Quaternion.LookRotation() to orient the weapons towards my target point each frame. Is that what you're currently using? You can also implement spread by using Random.Rotation and Quaternion.Lerp (Lerp between your ai$$anonymous$$g rotation and your random rotation, with t chosen to deter$$anonymous$$e the final spread, where a value of 1 means a potential spread of 180 degrees (meaning all the way in the opposite direction), 0.5 -> 90, 0.25 -> 45, etc.

avatar image PanzerPotato ImpOfThePerverse · Jun 02, 2018 at 06:40 PM 0
Share

Yeah I was initially using LookRotation(), but I switched to using a empty transform in the same position, with LookRotation() at the target. I then rotated my turret to the angle of that empty transform. The inaccuracy problem I fixed, but thanks though!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Nerull22 · May 27, 2018 at 09:22 PM

So here are my immediate thoughts on the subject. I'm assuming you have a Vector3 as your aiming location. So having your aiming system send that exact aiming location to each individual gun. I would draw an absolute gizmo for aiming. Meaning that it should store the exact point it should be trying to hit. Then a separate color line that draws where it's trying to aim. That way you know that it's aiming at the right point, but when you have the spread applied you can see visually in your scene that it's trying to hit something and the spread is what you want.


Now give each individual gun a maximum spread amount. Treat this value as a radius; so half of what you want the actual spread to be. Then do a random range between 0 and this value. Multiply that by Vector3. One as a constant value can be multiplied by an entire matrix and then add your result to the definite aiming location. That will give you your offset aiming Vector3. I'll write some code below that more details this out as I'm sure it's hard to follow in the text. I'll explain some of the content of the answer below in case people don't understand some concepts.

     [SerializeField]
     private float _aimingSpread = 1;
 
     public Vector3 GetAimingPosition(Vector3 definiteAimingLocation)
     {
         float randomSpread = Random.Range(0, _aimingSpread);
         Vector3 spreadAimingLocation = Vector3.one * randomSpread;
         spreadAimingLocation += definiteAimingLocation;
 
         return spreadAimingLocation;
     }

Mind you that this has not been tested, but I'll explain the thought process.

So "definiteAimingLocation" will be the value that you're aiming location will be trying to hit. This is the exact Vector3 that you want each gun to try and hit.
The "_aimingSpread" will be a value in the gun of its potential inaccuracy. This is a radius of spread, so make sure it's half of what you want.
So you'll generate a random spread that will act as your inaccuracy. Then you'll multiply this by Vector3.one which will give you how off the gun will be. Vector3.one * 0.5f will equal (0.5f, 0.5f, 0.5f). Constants times Matrices will apply to every value in the matrix.


Then add that Vector3 to the definite aiming location and it will give you the Vector3 of the aiming spread location. So let's say you're trying to hit (2, 5, 6.5) and your spread generated a Vector3 of (0.5f, 0, -1). Adding these together will give you your final aiming position of (2.5f, 5, 5.5f). Aiming the gun there will give you that spread you're looking for.
I know this is long and probably repetitive, but I hope it helps.

Comment
Add comment · Show 4 · 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 PanzerPotato · Jun 02, 2018 at 06:47 PM 0
Share

Currently I do have a script on my camera that is using a raycast to reposition a transform (which I have my guns ai$$anonymous$$g at). But as for the dispersion, you do have a pretty interesting concept (which I think with a little modification could be splendid). But using Vector.one, wouldn't that cause the dispersion to be diagonal (eg. ai$$anonymous$$g at 1,1,1 or 5,5,5 or -2,-2,-2)?

avatar image Nerull22 PanzerPotato · Jun 04, 2018 at 04:17 AM 1
Share

Sorry for the late reply. Yes, you're right. That would probably not be what you're looking for. You would need to generate a random Vector3. Sorry, wrote that a little quickly and didn't test it. You may even want to do something like a total inaccuracy. Like all your random can not exceed a maximum innacuracy of 3 units off. So your offset could be (2,0,1) but no more. Can have some interesting rule sets in there. But yes, you'll have to generate a random Vector3. $$anonymous$$y bad. :)

avatar image PanzerPotato Nerull22 · Jun 05, 2018 at 01:38 AM 0
Share

It's all right! I took what you said and got a reliable dispersion system, and have now moved on to other problems. But thanks for your help!

Show more comments

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

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

Why wont my script detect the input of my controller? 1 Answer

How to add fireRate to my gun script? (Need Help) 1 Answer

My gun shoots while I'm reloading 2 Answers

Need help with burst fire script / adjusting the amount of damage my gun does. 0 Answers

Particle System isn't playing 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