• 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
1
Question by DSebJ · Feb 14, 2017 at 12:39 AM · direction

How can I add two Vector3 directions together?

I have a direction to my target object, which is in a Vector3 format. I want cast a number of rays, as offsets from this initial direction.

X is the object. D is the direction the player needs to travel in. A & C are other directions that I want to cast rays towards to check for impacts.

 A    D    C
  \   |   /
   \  |  /
    \ | /
     [x]

I was trying to add an offset, such as Vector3.Right to the Direction and then normalise the result, however this only works if everything is oriented towards World Space.

So how can I convert and add a direction (Vector3.right) as an offset to an existing direction?

I'm fairly sure I just have World Space and Local Space directions mixed up but can't see an easy way to convert between this with Vector3?

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

2 Replies

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

Answer by jdean300 · Feb 14, 2017 at 01:29 AM

Have you looked at transform.right? This will be relative to the transforms current rotation

 Vector3 heading = transform.forward;
 Vector3 rightCheck = heading + (transform.right * .5f);
 Vector3 leftCheck = heading - (transform.right * .5f);

rightCheck is C from your diagram, leftCheck is A. You may need to normalize the vector depending on what you're doing exactly. Multiplying by .5f should give you a 22.5 degree angle, so adjust it as needed depending on the angle you want.

Comment
Add comment · Show 5 · 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 DSebJ · Feb 14, 2017 at 01:36 AM 0
Share

Thanks - but is there a way to do this without using the original transform? I'm using this in a recursive search algorithm and the checks are occurring many layers away from the original object.

avatar image jdean300 DSebJ · Feb 14, 2017 at 01:43 AM 1
Share

Well that's going to depend on what information you have available.

You can rotate a vector by multiplying it by a quaternion. For example, rotate around x axis by 60 degrees: Vector3 rotated = new Quaternion.Euler(60, 0, 0) * unrotated.

To move from local space to world space you just need to add a transforms position, or subtract to do the opposite.

Other than that I can't tell you exactly what to do unless I see some code.

EDIT: oops, missed the unrotated vector in the Quaternion rotation example

avatar image DSebJ jdean300 · Feb 14, 2017 at 01:54 AM 0
Share

Thanks - It's hard to split out as it's all embed but here are the items I have:

I have Starting Point which is a Vector3 and has the position I am trying to cast rays from. The heading towards the target from that position (as a Vector3) and a GameObject reference of the Target Object.

I was trying this

  RaycastHit RayCastHitInfo = new RaycastHit();
         bool hit = Physics.SphereCast(StartingPoint, SphereCastRadius, heading, out RayCastHitInfo, (RouteSegmentLength + RouteSegmentBuffer), -1, QueryTriggerInteraction.Ignore);

and defining the headings in a calling method with:

 TestRoutePoints.Add(TestRoutePoint(StartingPoint, (heading +  new Vector3(0.5f, 0, 0)).normalized, TargetObject));
                     TestRoutePoints.Add(TestRoutePoint(StartingPoint, (heading + -new Vector3(0.5f, 0, 0)).normalized, TargetObject));


Show more comments
avatar image
0

Answer by Glurth · Feb 14, 2017 at 02:28 AM

In your diagram, your Vector happens to be pointing directly up. In THIS case your can use Vector3.right and Vector3.left, to get those offsets you want to add.

However if your vector is at some arbitrary angle, you will need to use some kind of rotated version of Vector3.right and left. The important thing to note, it that those offset vectors need to be Perpendicular to your initial vector.

There are an infinite number of vectors that are perpendicular to a Vector3 (think; a disk of directions at the end of the vector). This is unlike a Vector2 you might draw on paper, which has only 1 perpendicular line.

I would probably use Vector3.Cross product to compute this vector, but this will require another Vector that will be perpendicular to BOTH. I think there is also shortcut like PerpVector= new Vector3(-vec.z,vec.x,vec.y); to get a perpendicular vector, but I've not tested that...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

66 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

Related Questions

Make a moving object continue it's movement from the same direction , even it reaches target 1 Answer

Move into direction of another object 1 Answer

How to Get My Camera to Follow First Person POV in First Person Controller 1 Answer

Moving Randomly 1 Answer

"Fake" Vector3.forward? 3 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