- Home /
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;
}
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.
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

Follow this Question
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