I have a projectile that i want to seek a target. I don’t want to use the look at function because i don’t think i can change how quickly it looks at the target.
I have my projectile constantly moving forward:
transform.Translate(new Vector3(0,0,-(ProjThrust * Time.deltaTime)));
so no matter what orientation it is, it will move forward.
Then i plan to just rotate the projectile up/down or left/right (by a certain variable amount) to seek my target.
At first i tried splitting up my rotations into up/down and left/right, by defining a triangle encompassing these two “planes” i made. If i can determine the angle to the target i can figure out of my target is to the left or right of my target by using its location.
Now i tried using this:
to find out how to figure out the angles on both of my axis and then rotate the direction needed based on the angle. This method doesn’t seem to work.
Next i tried making a triangle out of each position’s x and z locations to make a plane and a triangle to figure out the angle. i used:
float SideO = Mathf.Abs(transform.position.z - targetLocation.z);
float SideA = Mathf.Abs(transform.position.x - targetLocation.x);
float targetAngle = Mathf.Atan(SideO / SideA) * Mathf.Rad2Deg;
This picture shows what i mean by by my horizontal plane, the z is used for my depth but if i look at it form the side i can form a triangle with this data, and then calculate the angle and then if my x value is larger than my target (more right) i turn left, and i turn right if my x value is smaller.
This one describes the vertical movement, using z again for depth. And using the same kind of idea, if my y is less i move up, and i move down if my y is bigger.
Obviously i would check angles and see if i am within a threshold to be looking at my target, but tan doesn’t work well with negative values, and neither does the unity angle functions (they wrap around 180 degrees or pi rads).
How would i use my current methods to determine how to rotate my projectile?
Am i using the correct axis, or are the axis inconsistent if it rotates, and if so how do i determine axis of the object in world space and also in local space (to determine angle). I am confused on this.
I hope i explained this well and the pictures aren’t too ugly. I don’t know if this system has a “spoiler” or “image” hider tags.