Version:
Unity: 2022.2.5f
Burst: 1.8.2
Entities: 1.0.0-pre.15
Hello
I am trying to make my ai use my current animations but the way have have made it is more based on the input from a player. But that would work a bit wierd for my ai character, especially since i want my ai to be able to strafe.
So what i need to calculate the floats that drives my animations based on the movement of the ai.
I have an naive solutions for this, just testing positions in 360 and check which values are closest to the movement direction.
Another solution that i am thinking about is just to calculate verticalMovement and horizontalMovement based on movement direction, speed and also take the look direction into consideration. But i not sure how i would calculate that with vectors and possible quaternion.
translateData.position = translateData.position + (characterControllerData.verticalMovement * math.forward(translateData.rotation)) * 4f * deltaTime;
translateData.position = translateData.position + (characterControllerData.horizontalMovement * vectorRight) * 4f * deltaTime;
Red is movement, orange is looking direction
Player input
float sensitivity = 0.05f;
//Inputs
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
bool mouseLeftClick = Input.GetMouseButtonDown(0);
bool leftAlt = Input.GetKey(KeyCode.LeftAlt);
bool jumpButton = Input.GetKey(KeyCode.Space);
bool crouchButton = Input.GetKey(KeyCode.C);
bool phroneButton = Input.GetKey(KeyCode.Z);
//Take input
Entities.ForEach((ref PlayerInputData playerInputData) =>
{
playerInputData.inputHorizontal = horizontal;
playerInputData.inputVertical = vertical;
playerInputData.inputRotationMouseX = mouseX;
playerInputData.inputRotationMouseY = mouseY;
playerInputData.inputMouseLeft = mouseLeftClick;
playerInputData.inputLeftAlt = leftAlt;
playerInputData.inputJump = jumpButton;
playerInputData.inputCrouch = crouchButton;
playerInputData.inputPhrone = phroneButton;
//Debug.Log(" Player Input " + " horizontal " + horizontal + " vertical " + vertical);
}).WithBurst().Schedule();
//From player input to character controller
Entities.ForEach((ref CharacterControllerData characterControllerData, in PlayerInputData playerInputData) =>
{
characterControllerData.horizontalMovement = playerInputData.inputHorizontal;
characterControllerData.verticalMovement = playerInputData.inputVertical;
characterControllerData.rotationDirectionX = characterControllerData.rotationDirectionX + (playerInputData.inputRotationMouseX * sensitivity);
characterControllerData.rotationDirectionY = characterControllerData.rotationDirectionY + (playerInputData.inputRotationMouseY * sensitivity);
characterControllerData.useWeapon = playerInputData.inputMouseLeft;
characterControllerData.jump = playerInputData.inputJump;
characterControllerData.crouch = playerInputData.inputCrouch;
characterControllerData.phrone = playerInputData.inputPhrone;
//Debug.Log(" Character Input " + " horizontalMovement " + characterControllerData.horizontalMovement + " verticalMovement " + characterControllerData.verticalMovement);
}).WithBurst().Schedule();
Translation system
public partial class TranslationSystem : SystemBase
{
protected override void OnUpdate()
{
float deltaTime = SystemAPI.Time.DeltaTime;
//Character controller tranfer translate data
Entities.ForEach
((ref TranslateData translateData, in CharacterControllerData characterControllerData) =>
{
//Rotation
translateData.rotation = quaternion.RotateY(characterControllerData.rotationDirectionX);
//Movement
float3 vectorRight = math.mul(translateData.rotation, math.right());
vectorRight.y = 0;
vectorRight = math.normalizesafe(vectorRight);
translateData.position = translateData.position + (characterControllerData.verticalMovement * math.forward(translateData.rotation)) * 4f * deltaTime;
translateData.position = translateData.position + (characterControllerData.horizontalMovement * vectorRight) * 4f * deltaTime;
//Debug.Log(" characterController " + " translateData.position " + translateData.position);
}
).WithBurst().ScheduleParallel();
//Moving and rotating objects
Entities.ForEach
((ref LocalTransform localTransform, in TranslateData translateData) =>
{
//Set TransformAspect
localTransform.Position = translateData.position;
localTransform.Rotation = translateData.rotation;
//Debug.Log(" Moving objects " + " localTransform.Position " + localTransform.Position);
}
).WithBurst().ScheduleParallel();
}
}
Character system for setting animation and moving the Mono GameObject
public partial class CharacterControllerGeneralSystem : SystemBase
{
protected override void OnUpdate()
{
float deltaTime = SystemAPI.Time.DeltaTime;
float animationAccelerationSpeed = 0.05f;
//Set animation Data
Entities.ForEach
((ref CharacterAnimationData characterAnimationData, in CharacterControllerData characterControllerData) =>
{
/*
//HorizontalAxis
if (characterControllerData.horizontalMovement > 0.05f)
{
characterAnimationData.velocityX = characterAnimationData.velocityX + animationAccelerationSpeed;
if (characterAnimationData.velocityX > 1)
{
characterAnimationData.velocityX = 1;
}
}
if (characterControllerData.horizontalMovement < -0.05f)
{
characterAnimationData.velocityX = characterAnimationData.velocityX - animationAccelerationSpeed;
if (characterAnimationData.velocityX < -1)
{
characterAnimationData.velocityX = -1;
}
}
//VerticalAxis
if (characterControllerData.verticalMovement > 0.05f)
{
characterAnimationData.velocityY = characterAnimationData.velocityY + animationAccelerationSpeed;
if (characterAnimationData.velocityY > 1)
{
characterAnimationData.velocityY = 1;
}
}
if (characterControllerData.verticalMovement < -0.05f)
{
characterAnimationData.velocityY = characterAnimationData.velocityY - animationAccelerationSpeed;
if (characterAnimationData.velocityY < -1)
{
characterAnimationData.velocityY = -1;
}
}
*/
characterAnimationData.velocityX = characterControllerData.horizontalMovement;
characterAnimationData.velocityY = characterControllerData.verticalMovement;
}
).WithBurst().ScheduleParallel();
//Reading from CharacterControllerData and setting values for GameObject and Animator
Entities.ForEach
((CharacterControllerClass characterControllerClass, in CharacterAnimationData characterControllerData, in LocalTransform localTransform ) =>
{
characterControllerClass.animator.SetFloat("VelocityX", characterControllerData.velocityX);
characterControllerClass.animator.SetFloat("VelocityY", characterControllerData.velocityY);
characterControllerClass.characterControllerGO.transform.position = localTransform.Position;
characterControllerClass.characterControllerGO.transform.rotation = localTransform.Rotation;
}
).WithoutBurst().Run();
}
}
Components
public struct CharacterAnimationData : IComponentData
{
public float velocityX; //Sidways movement X
public float velocityY; //Forward or backward Y
}
public struct CharacterControllerData: IComponentData
{
public float horizontalMovement; //Sidways movement X
public float verticalMovement; //Forward or backward Y
public float rotationDirectionX;
public float rotationDirectionY;
public bool useWeapon;
public bool jump;
public bool crouch;
public bool phrone;
public float maxSpeed;
}
public class CharacterControllerClass : IComponentData
{
public GameObject characterControllerGO;
public Animator animator;
}
Mono test
void Update()
{
float sensitivity = 1f;
float deltaTime = Time.deltaTime;
if (isPlayer)
{
horizontalMovement = Input.GetAxis("Horizontal");
verticalMovement = Input.GetAxis("Vertical");
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
rotationDirectionY = rotationDirectionY + (mouseX * sensitivity);
}else
{
aiInput(gameObject, directionGO, targetLookGO, rotationDirectionY, verticalMovement, horizontalMovement); // Not working
}
updateMovement(gameObject, rotationDirectionY, verticalMovement, horizontalMovement);
}
void updateMovement(GameObject gameObject, float rotationDirectionY, float verticalMovement, float horizontalMovement)
{
float deltaTime = Time.deltaTime;
Quaternion rotation = (Quaternion.AngleAxis(rotationDirectionY, Vector3.up));
Vector3 rightMovement = (rotation * Vector3.right);
rightMovement.y = 0;
rightMovement = Vector3.Normalize(rightMovement);
Vector3 forwardMovement = (rotation * Vector3.forward);
gameObject.transform.rotation = rotation;
gameObject.transform.position = gameObject.transform.position + (verticalMovement * forwardMovement) * 4f * deltaTime;
gameObject.transform.position = gameObject.transform.position + (horizontalMovement * rightMovement) * 4f * deltaTime;
Debug.Log("rightMovement " + rightMovement + " forwardMovement " + forwardMovement);
}
Any suggestions on how to proceed with this problem is appreciated