- Home /
GameObject facing hit.point doesn't always work
I've got a script that makes a character turn to face the hit.point then move towards it while playing a walk animation. This all works great, most the time. Sometimes, when it is commanded to walk somewhere, it will walk backwards or diagonally. This usually happens if you press different areas quickly or if you press close behind the character when its stationary. The piece of relevant code is as follows:
private var pointToGo : Vector3 = Vector3.zero
function Update ()
{
if(Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
if(hit.transform.name == "NavBar")
{
Zombie.transform.rotation = Quaternion.LookRotation(transform.position - hit.point);
I've put the function before the move to hit.point and the animations so I'm not sure why it is doing this. Anyone know the answer?
Any chance the pivot point is not in the center of the character? Can we see the movement code as well?
The code that comes after what I've already posted is as follows:
pointToGo = hit.point;
}
}
}
Zombie.transform.position = Vector3.$$anonymous$$oveTowards(Zombie.transform.position, pointToGo, Time.deltaTime * speed);
if(Zombie.transform.position==pointToGo)
is$$anonymous$$oving=false;
if(is$$anonymous$$oving){
ZombieAnim.animation.CrossFade("Walk Legacy");
}
else{
ZombieIdle.animation.CrossFade("Idle Legacy");
}
Answer by FinKone · Apr 14, 2014 at 11:49 AM
Check to make sure that you are hitting NavBar (debug the name of what youve hit) if its just for moving perhaps ignore all layers with the raycast except NavBar. You could also make this block of code its own method incase you need to use it in something else. Tried to leave this as a comment, phones not seeing the option. Let me kmow if this is the issue if not I'll try to help out more.
I've only got the character and the NavBar (which is an invisible Plane) in the scene so it can't be hitting anything else. It's looks like it doesn't have enough time to turn around so just goes in the direction its already facing in. Is there a way I could say "When hit.point is detected, rotate to face it first, then once that is finished, move towards hit.point"?
@hollym16 - ti$$anonymous$$g is likely not your issue. Your last line of code causes an immediate rotation.