- Home /
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.
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
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.
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
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
try getting the...?? I think you missed a word, I will take a look to tangent. thank you
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
.
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;
}
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;
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
Follow this Question
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