- Home /
Alternatives to LookAt()
Hi im creating an AI Script and i am starting to look away from the dodgy things i started out with such as the LookAt() for Ai.
reason being is i cant use velocity to track its rotation to play the appropriate animations and i want to learn something new.
what i have tested is
Cross Product Dot Product and also manually altering the rotations of the enemy, i have successfully done so using a rigidbody with Getcomponent().angularVelocity , this also plays the correct animation when done so, but the issue im having is getting the enemy to stop rotating when he is facing the player, i know i can hack this in with a simple raycast, if hit player move forward and stop rotating, but the issue with that approach would be when its not hitting the player how does it know how to turn CW or CCW ??
so i have been digging for a different approach and thought i would turn to the community for some help with this.
essentially i have a few waypoints on the map to test with atm but i cant seem to get the AI to face the object, and i do not want to resort in using Transform.LookAt() as it defeats the purpose. and can not play animations such as rotating as it locks to the object and does not Slerp to it. i do see this question posted a fair bit, but their never seems to be an answer to the question that works.
Thanks in advanced for taking the time to pitch in to this topic.
what exactly do you want to do? Slerp an objects rotation to face another object?
I'm creating a soldier AI, I had made a small prototype, although it worked with the legacy animation and the navmeshagent AI unity supplies. I want to create it from complete scrap. That goes for movement and sight of the AI. So starting small. Like we do 1 step at a time, since this sort of rotation is something I strugle most with I thought I would ask for some help with it.
The rotation I'm after is very similar to the look at. But I need to know when I'm actually looking towards the target so I can apply tthe correct movement. $$anonymous$$g not facing target and running sideways. I plan on adding 8directional movement for AI. Basic forward back, left right. And strafing. All while looking towards the target the AI can still move freelyaround the map while firing at the player without snap locking. Which transform.LookAT does.
Answer by globalenemy · Jan 28, 2016 at 01:23 PM
Your, idea to approach the problem via raycast isn't too bad. well maybe it's a bit loaded. You can simply calculate an angle from the Enemy to the Player and check this back with the current rotation of the Enemy.
Since you're not using RotateTowards or LookAt, you might get into the problem, that the Enemy will never actually reach the desired rotation. Causing it either to continue rotating or switching between rotating back and forth very close to the target angle, depending on your code.
I might be talking bullshit here, but your problem sounds very similiar to one I had some time ago: http://answers.unity3d.com/questions/1088611/how-to-do-quaternionrotatetowards-on-single-local.html#comment-1089733 (the linked comment shows the solution to the back and forth problem.