Hello, I am trying to make a game similar to Rimworld or Prison Architect, so I have a AI controlled sprite character with 4 different sprites for each main direction and I'm currently trying to calculate the target direction via Vector3.Dot and then assign the correct sprite:
void Update()
{
float dotUp = Vector3.Dot(_navMeshAgent.destination - _thisTransform.position, Vector3.forward);
float dotRight = Vector3.Dot(_navMeshAgent.destination - _thisTransform.position, Vector3.right);
if(dotUp > 0.8f)
{
// walking up
spriteObject.GetComponent().sprite = sprite_Back;
}
else if(dotUp < -0.8f)
{
// walking down
spriteObject.GetComponent().sprite = sprite_Front;
}
else if(dotRight > 0.1f)
{
// walking right
spriteObject.GetComponent().sprite = sprite_Right;
}
else if(dotRight < -0.1f)
{
// walking left
spriteObject.GetComponent().sprite = sprite_Left;
}
}
The problem is that if the character has to change direction from left to right or vice versa and if he runs a bit up or down diagonally, he ends up looking up or down first before watching right/left. Is there a way that I can use the Vector3.Dot product to make it behave as it should?
Here's a Web Player demo which should clarify what I mean:
[WebPlayerDemo][1]
[1]: https://39e3d49a5c1e1fab3ca77bcf68237ce25d6c3a97.googledrive.com/secure/AIMa9voQKgJZcxHAvAJn9zCCZZdIR7d5VshNn8hYG3eIiS5RtYX68q0Jub_KKMohc3RB7ZVxO26JjTVJemmFmdPj9DjNl-sUZb4Uz5dS4jiL8Hdh0EuJ1iaqjmSgdwFe_ONMZ0pnyo3G7LXmzqsizMeHNmcmxWDvPL8nWkdHTosVlTaEWTfBymn8YfkznjxM8b1kvva7M0Q-I-2848EA8IVspaqYoK9ID47K6W7oxjwOZGcLKR0rGt8bN7F6v24QHSbD6tDRa25XeNFCYIvN2VuRsaPKjsZcIdZd8bgMqZZTbj0ysgxUoDGLNt25UGp5v0ZanxEs4vG7mEMAhFSlbKxuWw2zfwhhSJrhwJLIcU4UGIbNXZ3UjikST48mmvms0L_9k5D06A3BO68FtPo4tpx5ZGxITx7JxHjna-RQw44u1KALGVn508N5CYIYJJ9QdLnd8DksAmfSziWF8FP0OOiGtKOeA0uExgFmO6hx6_ftEYLjRv5nujMp1MHV0b_ZphZNjUerkmYHWJyho0DNq1XJ-CQTOmajUlydpC4UVUC0vGZYnBto7OYC6yL32BFOlKhOPJwKdr2KFdXoHx2xSU2J-HYyVTNg1A==/host/0B00QDfR1-SBzfkVvaEhzb0Njc0tiWlRnSldTOWtmTkFfN1pvd3Awa2lSSGR4VVlERUswUUE/Zauberlehrling_WebBuild.html
↧