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
0
Question by CaKeMeaT · Jun 30, 2015 at 02:44 AM · trigonometry

Get X,Z coordinates based on origin and angle

I'm having a little trouble applying the math I need for my fireballs to spread out.

NO matter what I do the projectiles travel strangely. when facing certain directions the 3 fireballs begin and travel in a line, and then at others they spread out nicely. The way I figure is that I need to get the appropriate distance across x and z axis based on cosine/sine of the distance from the player (transform)(6.0f) in this example...

I was trying to use trig, but obviously messed that up too. Help?

My scratch notes are below, but even these failed me:

             /*
             float add_x = Mathf.Cos(shoot_angle.x ) * 6.0f;
             float add_z = Mathf.Sin(shoot_angle.z ) * 6.0f;

             float tmp_x = _spell_source.position.x + add_x;
             float tmp_z = _spell_source.position.z + add_z;
             float tmp_y = _spell_source.position.y;

             Vector3 origin_c = new Vector3(tmp_x, tmp_y, tmp_z);
             */



relevant code below:

             for(int c = 0; c < 3; c++) {
                 GameObject fireball_clone;
                 fireball_clone = Instantiate (fireball_pre_src, _spell_source.position, _spell_source.rotation  ) as GameObject; // + new Vector3( (2.0f * c) -2.0f , 0, 0)
                 Vector3 shoot_angle = _spell_source.forward + new Vector3(0, 0, (0.2f * c) -0.2f );
                 fireball_clone.GetComponent<Rigidbody> ().velocity = shoot_angle * 800 * Time.deltaTime;
             }






alt textalt text

angled.png (184.9 kB)
straight.png (194.9 kB)
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 maccabbe · Jun 30, 2015 at 02:56 AM

The code up to looks mostly right. However it seems like you're using Mathf.Cos/Mathf.Sin with degrees (0-360) when they expect values in radians (0-2*pi).

http://docs.unity3d.com/ScriptReference/Mathf.Cos.html

You can convert from degrees to radians using Mathf.Deg2Rad, i.e. float radians=angle*Mathf.Deg2Rad;

http://docs.unity3d.com/ScriptReference/Mathf.Deg2Rad.html

The picture at the bottom shows that the directions are correct but the speed is different for the left and right fireballs. You should probably normalize the direction vectors after adding to them so they all have a magnitude of 1.

http://docs.unity3d.com/ScriptReference/Vector3.Normalize.html

http://docs.unity3d.com/ScriptReference/Vector3-normalized.html

However it would probably be simpler to rotate the vector using a quaternion which means it will be easier to understand in the future and the difference is speed would be negligible. For instance

 Vector3 shoot_angle1 = Quaternion.Euler(0, 20, 0)*_spell_source.forward;
 Vector3 shoot_angle2 = Quaternion.Euler(0, 0, 0)*_spell_source.forward;
 Vector3 shoot_angle3 = Quaternion.Euler(0, -20, 0)*_spell_source.forward;

would return the direction rotated 20, 0, and -20 along the global y axis.

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 CaKeMeaT · Jun 30, 2015 at 06:23 AM 0
Share

Thank you. you set me straight. Thanks for links too. thoughtful answer.

Final Solution ended up being this:

             for(int c = 0; c < 3; c++) {
                 GameObject fireball_clone;
 
                 Vector3[] shoot_angles =  new Vector3[3];    
 
                  shoot_angles[0] = Quaternion.Euler(0, 20, 0)*_spell_source.forward;
                  shoot_angles[1] = Quaternion.Euler(0, 0, 0)*_spell_source.forward;
                  shoot_angles[2] = Quaternion.Euler(0, -20, 0)*_spell_source.forward;
 
                 fireball_clone = Instantiate (fireball_pre_src, _spell_source.transform.position, _spell_source.transform.rotation  ) as GameObject; // + new Vector3( (2.0f * c) -2.0f , 0, 0)
 
                 fireball_clone.GetComponent<Rigidbody> ().velocity = shoot_angles[c] * 900 * Time.deltaTime;
 
             }

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

21 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

Related Questions

Fixed distance between objects? 2 Answers

Help understanding transform 1 Answer

Radius of a Non-Uniform Sphereoid/Ellipsoid at a Given Surface Point? 1 Answer

C# - Day / Night Cycle - Sun Moving @ 4x Speed? 3 Answers

Create a Triangle Mesh. 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