Hi everyone!
I’m working on an AR app in which users are able to add 3D models of robots to the AR visualization, and can also make them move and rotate with the use of a GUI. In this GUI, users can make the robots rotate around their own Y axis by entering the rotation in degrees, and can make them move forward by entering the number of “distance units” they want the robot to move.
The rotation works fine, but I’m having a problem with the forward movement: the robot model (which is a gameObject) keeps going forward infinitely, no matter how many “distance units” it was programmed to move or which direction it is facing.
Basically, I’m using a “targetPosition” variable, the value of which is the result of the robot’s “transform.position” vector added to the “move forward” vector (which is basically (0, 0, moveUnits)). Then, I use a conditional loop to determine if the robot’s “transform.position” is equal to “targetPosition”. While this condition has not been met, the robot keeps moving forward by using “transform.forward”.
This is my current C# code for the forward movement (all the variables are global and were previosly declared in the code):
public void PreMove(float moveUnits)
{
moveVector = Vector3.forward * moveUnits;
targetPosition = transform.position + moveVector;
//this state calls the Move() function below, which runs inside FixedUpdate():
state = robotState.Move;
}
public void Move()
{
//checks if the robot's current position is equal to the target position:
if (transform.position != targetPosition)
{
transform.position += transform.forward * Time.deltaTime * moveSpeed; //moves the robot forward
}
else
{
state = robotState.Idle; //changes the robot state to neutral
}
}