- Home /
Find a position forward of GameObject with possible angle
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.
I believe I found the answer I haven't tested all use cases yet but consider this function
private void setDestinationAndDistance(int angle, float distance)
{
// local coordinate rotation around the Y axis to the given angle
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.up);
// local coordinate to world coordinate rotating direction forward
Vector3 translateToWorldCoordinates = transform.TransformDirection(rotation * transform.forward);
// add the desired distance to the direction
Vector3 addDistanceToDirection = translateToWorldCoordinates * distance;
// add the distance and direction to the current position to get the final destination
this.destination = transform.position + addDistanceToDirection;
}
Well it almost worked, I put this one a cube mesh and the cube moves along the points if I do not rotate the cubes and they stay perfect at rotation 0,0,0 once I start to rotate the cube to the position they are going things go wrong with subsequent calls. The target destinations start jumping all over the place after three calls. I tested this with 12 calls with 30 degrees which should move the cube in a circle, however this is not what happens. It's hard to describe what is going on but it seems the angles start jumping around. So this isn't the solution to the problem yet :(
Are you sure your GameObject isn't child to an object that has a scale component to its transform, or that there isn't scaling anywhere in its hierarchy?
There was some actual scaling it was more a long brick then a cube 1, 1, 2 was the scaling but I found the solution a small while ago, it's the current accepted answer. I believe it is correct
Hum, I can't see any answer. $$anonymous$$aybe it's in the mod queue.
Well they removed the answer it seems , maybe I can't answer my own question like on stackoverflow ? When I get home from work I'll try posting it again. The solution seems to work, my cubes move as they should.
Answer by Celludriel · Apr 20, 2017 at 04:56 PM
Turns out I didn't need to transform to world coordinates. This solution seems to work. I tried with several degrees
public void Move(float degrees, float distance)
{
// local coordinate rotation around the Y axis to the given angle
Quaternion rotation = Quaternion.AngleAxis(degrees, Vector3.up);
// add the desired distance to the direction
Vector3 addDistanceToDirection = rotation * transform.forward * distance;
// add the distance and direction to the current position to get the final destination
this.destination = transform.position + addDistanceToDirection;
Debug.DrawRay(transform.position, addDistanceToDirection, Color.red, 10.0f);
transform.LookAt(this.destination);
}
I tried to understand Quaternions from wiki and some other sources but I just can't get a grip on complex numbers, so might be above my head. I hope this will help people finding locations from one spot to another in an angle.
Your answer
Follow this Question
Related Questions
How would you sort gameobjects by their position in a 3D grid? 1 Answer
How can I move this part in a rectangle when I press a key? [C#] 1 Answer
How can I set the default position of a 3D object? 2 Answers
Placing object based on child relative to another gameObject. 1 Answer
Position next to other item 0 Answers