This question has been asked in one way or another over several years but none of the responses on them seem to give me the required result. Most of the time because the question is not clear enough either. So here goes my attempt in trying to ask clearly what the problem is.
I have a GameObject, in this case a capsule mesh, sitting at coordinates (0.0,1.0,-20.0) with rotation (0,0,0). I want to rotate and move to a position 10f units forward in an angle. Here are a couple of use cases:
move 10f forward in a 0° angle
move 10f forward in a 15° angle
move 10f forward in a -15° angle
So I need to find the target destination relative from (0.0,1.0,-20.0) to distance forward in x angle. I have tried a couple of implementations based on past answers. Lets take the basic use case 10f on a 0° angle so basically 10 units forward. So the end destination should be (0.0,1.0,-10.0)
this one gives me as destination (0.0, 0.0, 4.0). I lose my y axis from 1 to 0 and the z coordinate is way of.
private void setDestinationAndDistance(int angle, float distance)
{
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
this.destination = rotation * (Vector3.forward * distance);
}
This one gives me (1.0, 0.0, 3.9). Again I lose my y axis and it’s still wrong with all the rest
private void setDestinationAndDistance(int angle, float distance)
{
this.destination = Quaternion.AngleAxis(angle, Vector3.up) * Vector3.forward * distance;
}
Next I tried another proposed solution ending me up with (0.0, 0.0, 4.0) which seems familiar to my first result
private void setDestinationAndDistance(int angle, float distance)
{
Quaternion startAngle = Quaternion.Euler(0, angle, 0);
Quaternion qAngle = transform.rotation * startAngle;
this.destination = qAngle * Vector3.forward * distance;
}
I then turned to old circle math with radians to find an x, y coordinate in a Carthaginian grid. This works but it does not cover my usecase, this only works if my GameObject is pointing directly “North” aka 0° to begin with. If it would have been pointing 15° to the right and I do this on a 15° turn I won’t end up 30° from north but yep only 15°
private void setDestinationAndDistance(int angle, float distance)
{
float radians = angle * Mathf.Deg2Rad;
float y = Mathf.Cos(radians);
float x = Mathf.Sin(radians);
this.destination = transform.position + (new Vector3(x, 0, y) * distance);
}
This is where I realized I need more help and started another one of these questions, cause none so far are yielding desired results. Can anyone explain how to do it properly, keeping in mind I’m not native English and certainly won’t understand complex English math terms without wiki every one of them, where they are probably explained with even more complex English math terms … So math for dummies would apply here.