- Home /
Enemy Not Facing target direction (waypoint) correctly when moving between waypoints
I'm working on a 2D sidescroller where my Z axis is always "0". I'm importing FBX Models from Cheetah 3d with animations attached. I have the animations set to loop when the enemy is moving back and forth between waypoints. It's a simple waypoint system where the enemy travels to one waypoint then turns to face the second waypoint and travels to it (constant loop). Just a ping pong waypoint system (simple patrol for an enemy). The problem is that the character never faces the right direction when moving from waypoint to waypoint. For instance... one enemy is about 45 degrees off of facing straight ahead... and when he hits his waypoint and turns around he's 45 degrees off of facing straight heading in the other direction. Another enemy is completely faced backwards. He literally moves backwards from waypoint to waypoint. I am not sure if it's my import settings that are screwing this up or if I should be doing something in a script that I'm not. Here is the latest script that I'm working with... wrote several different scripts, tried tons of tutorials, and other scripts and no luck still... same thing over and over... anyhow here's what I got... if anyone has any suggestions or solutions in JS or C# I'd appreciate it (p.s. it makes no difference if I reference global or local positions):
var pathpoint : Transform[]; var currentPathPoint : int;
var speed : float = 5; var loop : boolean = true; var player : Transform; var searchPlayer : boolean = false; var viewDist : float = 10; var maxPathDist : float = 40;
function Awake(){ pathpoint[0] = transform; } function Update(){ if(currentPathPoint < pathpoint.length){
var target : Vector3 = pathpoint[currentPathPoint].localPosition;
var moveDirection : Vector3 = target - transform.localPosition;
var velocity = rigidbody.velocity;
var playerDist : Vector3 = player.localPosition - transform.localPosition;
if(moveDirection.magnitude < 1){
currentPathPoint++;
}
else if(searchPlayer == true){
if(playerDist.magnitude < viewDist){
velocity = Vector3.zero;
target = player.localPosition;
velocity = (player.localPosition - transform.localPosition).normalized * speed;}
if((player.localPosition - pathpoint[currentPathPoint].localPosition).magnitude > maxPathDist){
target = pathpoint[currentPathPoint].localPosition;
velocity = moveDirection.normalized * speed;
} }
else{ velocity = moveDirection.normalized * speed; } }
else{ if(loop){ currentPathPoint = 0; } else{ velocity = Vector3.zero; } }
rigidbody.velocity = velocity; transform.LookAt(target);}`
Answer by SneezingCatP · Apr 01, 2012 at 02:21 AM
is the enemy facing on the blue line??
Ah I see what you are saying... the front of my enemy needs to face forward in the "Z" direction (blue line)... I looked at my enemies and sure enough this is the issue... do you know the way to fix this? And why would my characters not line up correct in the first place?
Got it working... if anyone has this issue there appears to be a few ways to fix it... but I went back into Cheetah 3d and repositioned my object so it lined up straight with the Z axis.
This is an extremely common question, has been answered over and over, but I don't think "facing backwards" or "facing wrong way" is a keyword, so difficult to search.
$$anonymous$$ost common answer given is to child the wrong-facing object, adjust the facing, and have the parent do the ai$$anonymous$$g. "Best" answer is to fix it in the modelling program (but some plp aren't able to do that.)