Hey!
I have a project with artificial inteligence for my university. The problem basically looks like a 3d grid with some walls a goal and a moveable cube. The cube moves around the grid by rotating in the direction of the movement and moving one space at a time. I want to visualize this i already got the positions done correctly but the rotations are not quite working yet. Each rotation works correctly from the starting position. But when i rotate or move multiple times it gets buggy.
this is the part of the funtion that dose the calculating it is called once when i press an arrow to look at a previous or next state of the solution:
initialRotation = moveableObject.transform.rotation;
initialPosition = moveableObject.transform.localPosition;
MoveDirection LastMoveMade = currentState.LastMoveMade;
if (prevState != null)
{
switch (prevState.LastMoveMade)
{
case MoveDirection.Forward:
LastMoveMade = MoveDirection.Back;
break;
case MoveDirection.Back:
LastMoveMade = MoveDirection.Forward;
break;
case MoveDirection.Left:
LastMoveMade = MoveDirection.Right;
break;
case MoveDirection.Right:
LastMoveMade = MoveDirection.Left;
break;
default:
break;
}
}
switch (LastMoveMade)
{
case MoveDirection.Forward:
targetRotation = Quaternion.AngleAxis(90f, Vector3.left) * initialRotation;
break;
case MoveDirection.Back:
targetRotation = Quaternion.AngleAxis(90f, Vector3.right) * initialRotation;
break;
case MoveDirection.Left:
targetRotation = Quaternion.AngleAxis(90f, Vector3.back) * initialRotation;
break;
case MoveDirection.Right:
targetRotation = Quaternion.AngleAxis(90f, Vector3.forward) * initialRotation;
break;
default:
break;
}
elapsedTime = 0f;
the update looks like this:
private void Update()
{
elapsedTime += Time.deltaTime * animSpeed;
if (elapsedTime < animDuration)
{
float t = elapsedTime / animDuration;
Quaternion newRotation = Quaternion.Slerp(initialRotation, targetRotation, t);
Vector3 newPosition = Vector3.Lerp(initialPosition, targetPosition, t);
moveableObject.transform.rotation = newRotation;
moveableObject.transform.localPosition = newPosition;
}
else
{
moveableObject.transform.rotation = targetRotation;
moveableObject.transform.localPosition = targetPosition;
}
}