- Home /
Problem is not reproducible or outdated
move objects to a specific position relative to the player
Hi, I’m developing a game where you can command a group of soldiers and I’m finding it difficult to move them in formation in front of the player. I am using this function to derive the position that each soldier should assume in a specific formation
Vector3 CalcPosition(int index, int columns)
{
float posX = (index % columns) * space;
float posZ = (index / columns) * space;
Vector3 ret= transform.InverseTransformPoint(new Vector3(posX, transform.position.y, posZ+1));
return ret;
}
and i'm using MoveTowards to move soldiers from the position where they are to the position computed.
Unfortunately does not work, does anyone know how to do that?
"It does not work" is always the worst possible way to describe that something is not working. In what way does it not work? Is there an error? do they move to the wrong position? do they move at all? We have no idea what your problem is if you do not describe it properly.
I’m sorry I wasn’t specific. the problem is that soldiers doesn't move in the correct position.
Can you add an image to describe the current positions as well as the positions you want to achive?
Answer by Captain_Pineapple · Jul 08, 2021 at 08:33 AM
I guess your problem is that you got lost in the various InverseTransformX
functions.
what you probably need for your current situation is to change your current code to the following:
Vector3 CalcPosition(int index, int columns)
{
float posX = (index % columns) * space;
float posZ = (index / columns) * space;
Vector3 ret= transform.InverseTransformVector(new Vector3(posX, 0, posZ + 1));
return transform.position + ret;
}
this will create the vector as a base from origin to an imaginary position if your character stood in the origin with no rotation.
Then we apply the players rotation by InverseTransfromVector
Then we can add the players position as offset.
Let me know if that was the issue.
I tried the function you suggested but the soldiers do not assume the same position to rotate the player.
well ok i assumed that the script was on the player but judging from your description just now you'd need player.transform
instead of transform
in the script above.
yep, this script is located on the player. was just to be more accurate in the figure