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
2
Question by xJavier · May 10, 2014 at 04:07 AM · directionangle

Add angle to Vector3

Hi, my first question, maybe a very noob question but I need some help ...

let say that I want to shoot 3 bullets in different directions.

Please refer the img for reference.

alt text

Actually I´m using 3 diferent objects as targets, but I would like to have only one target and use angles to set the direction of the other 2.

this is what I'm using to get the direction:

 fireDirection = (targetPosition - originPosition).normalized;

How can I add n degrees to my fireDirection?

Thanks in advance for your help, also apologize for my bad english u.u

angles.jpg (22.7 kB)
Comment
Add comment · Show 5
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 DMGregory · May 10, 2014 at 04:51 AM 2
Share

In which axis do you want to rotate the firing direction?

In general, Quaternion.AngleAxis(angle, axis) * fireDirection will give you a rotated version of the vector.

avatar image xJavier · May 10, 2014 at 06:12 PM 0
Share

sorry you asked for the axis, my guess is it should be Z. it's for a platform game using 3D combined with 2D, but the character and enemies only move in X and Y

avatar image OrbitGames · May 10, 2014 at 06:19 PM 0
Share

try getting the , add an angle to it, then use Atan. if you don't know too much about tangent, i suggest researching this a bit, because it could be very useful

avatar image xJavier · May 10, 2014 at 07:05 PM 0
Share

try getting the...?? I think you missed a word, I will take a look to tangent. thank you

avatar image Graham-Dunnett ♦♦ · May 10, 2014 at 08:40 PM 0
Share

Often in games the up vector points in the y axis, so the little diagram you created is probably set in the xz plane. So, the fireDirection is approximately in the xz plane. If you take a cross product with y, you'll then get a vector that is perpendicular to fireDirection, call this perpDirection. Now you have two vectors that can help you generate the secondary target directions. Start at initial position, add on fireDirection and then add on perpDirection. The resulting vector is at 45 degrees to your fireDirection That's because both these vectors have unit length, since arctan(1,1)=45 degrees. Since you have a angle in $$anonymous$$d, take the tan of it, and you'll get a number. Scale perpDirection by this number. So, if you want a 30 degree secondary fire direction, tan(30)=0.577, so you need to do pos1 = initialPos + fireDirection + 0.557f * perpDirection. This vector pos1 is the point you need to fire your ray through. The other secondary ray needs to go through pos2 = initialPos + fireDirection - 0.557f * perpDirection.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xJavier · May 12, 2014 at 03:07 AM

I ended up with some trigonometry, thanks alot for all your help, you guys guided me to the solution.

here's the code, I've tested it with Debug.DrawLine

 Vector3[] CalculatePositionsForTripleDirection(){
         Vector3[] positions = new Vector3[3];
 
         float r = Vector3.Distance(bulletOriginPosition,targetPosition);
         float x = Mathf.Abs(bulletOriginPosition.x - targetPosition.x);
         float y = Mathf.Abs(bulletOriginPosition.y - targetPosition.y);
         float initialAngle = Mathf.Atan2(y,x) * Mathf.Rad2Deg;
         float secondAngle = (initialAngle + angleVariance) * Mathf.Deg2Rad;
         float thirdAngle = (initialAngle - angleVariance) * Mathf.Deg2Rad;
 
         //Calculate x2 and y2
         float x2 = r  * Mathf.Cos(secondAngle);
         float y2 = r  * Mathf.Sin(secondAngle);
 
         //Calculate x3 and y3
         float x3 = r  * Mathf.Cos(thirdAngle);
         float y3 = r  * Mathf.Sin(thirdAngle);
 
         //Verify if X is positive or negative
         if(targetPosition.x <  bulletOriginPosition.x){
             x2 = x2 *-1;
             x3 = x3 *-1;
         }
 
         //Verify if Y is positive or negative
         if(targetPosition.y < bulletOriginPosition.y){
             y2 = y2 * -1;
             y3 = y3 * -1; 
         }
 
         //Assign Values to positions
         positions[0] = targetPosition;
         positions[1] = new Vector3(bulletOriginPosition.x + x2, bulletOriginPosition.y + y2,targetPosition.z);
         positions[2] = new Vector3(bulletOriginPosition.x + x3,bulletOriginPosition.y + y3,targetPosition.z);
         return positions;
     }
Comment
Add comment · Show 2 · 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 robertbu · May 15, 2014 at 05:03 AM 3
Share

Here is an (untested) vector solution of your trig solution:

 Vector3 v = targetPosition - bulletOriginPosition;
 Vector3 v2 = Quaternion.AngleAxis(angleVariance, Vector3.forward) * v;
 Vector3 v3 = Quaternion.AngleAxis(-angleVariance, Vector3.forward) * v;
 
 positions[0] = targetPosition;
 positions[1] = bulletOriginPosition + v2;
 positions[2] = bulletOriginPosition + v3;
avatar image FredMastro robertbu · Jul 22, 2020 at 02:04 AM 0
Share

Vector3 v = targetPosition - bulletOriginPosition; Vector3 v2 = Quaternion.AngleAxis(angleVariance, Vector3.forward) * v;

I just tested this in my code, was looking for exact same thing, and it works. So consider it tested.

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

25 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

Related Questions

Adding force to an object based on its current angle. (2D) 1 Answer

Weird angle returned 1 Answer

Angle to Direction 1 Answer

Heat-seeking missile code. 1 Answer

How to calculate direction between 2 objects 2 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