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 Snubber · Apr 26, 2020 at 03:52 PM · vector3vector2

How can I add a Vector2 to a Vector3?

I have a Vector2 that indicates an "offset" from a Vector3. I want to add the Vector2 to the Vector3 such that the x and y of the Vector2 are being added along the normal plane of the Vector3.

I am trying to simulate a shotgun spread. The Vector3 is the direction of the shotgun and the Vector2 is the random spread (from Random.insideUnitCircle)

Any ideas?

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 endasil_unity · Apr 26, 2020 at 04:07 PM 0
Share

If you simply want to add the x and y from the Vector2 to the Vector3 you can do this: shotGunDir = new Vector3(shotGunDir.x + offset.x, shotGunDir.y + offset.y, shotGunDir.z);

avatar image Snubber endasil_unity · Apr 26, 2020 at 04:18 PM 0
Share

But I want the vector2 to be added to the normal plane of the vector3. Like it needs to be perpendicular to the vector3

avatar image endasil_unity Snubber · Apr 26, 2020 at 05:02 PM 0
Share

A i was too quick when reading your post. Sorry about that. Could you make a quick sketch and upload that demonstrates how you want the bullets to fly because i'm having a bit hard time getting a picture in my head what you want to archive.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Apr 26, 2020 at 05:17 PM

Well since you want to simulate bullet spread the approach you've menioned isn't a great solution. First of all by adding the vector to your direction vector you will also change the vector length. So the greater the offset vector the longer the over all vector gets. Of course the result can be normalized but the random distribution isn't uniform.


As it is with most issues usually there's already a solution out there somewhere. In this case you can use my GetPointOnUnitSphereCap method. It does something similar to Random.onUnitSphere but restricts the output to a certain orientation with a max angle which defines the size of the "cap" on the unitsphere.


The original question had a quite nice image illustrating what he wanted, unfortunately he used an external image hoster and the image got deleted.

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 Snubber · Apr 26, 2020 at 10:43 PM

I finally figured it out.

forward is the direction I am shooting in. rand is the Vector2 offset. I turned the Vector2 into a Vector3 and applied the rotation of the transform to it (the rotation must be on the left of the *) and added that to the forward direction.

  forward + (transform.rotation * new Vector3(rand.x, rand.y, 0))
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

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

Related Questions

Vector2 Normalize ? 3 Answers

Get RaycastHit Local Coords (C#) 1 Answer

Get a position relative to a gameobject 0 Answers

Vector2 vs Vector3 performance? 2 Answers

Rotate Local Space Vector to World Space with heading vector 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