- Home /
ROTATION BUG ERROR?
The enemy is rotating when I come close to target object. Relevant snippets and screenshots: Apparently it happens here, whenever I try to chase object:
private void Chase()
{
transform.LookAt(enemyOfenemy);
animator.SetBool("isRunning", true);
animator.SetBool("isWalking", false);
animator.SetBool("Idle", false);
nav.speed = runningSpeed;
nav.isStopped = false;
nav.SetDestination(enemyOfenemy.position);
nav.destination = enemyOfenemy.position;
}
Or Attack it:
void StopOnAttack()
{
if (isOnSelectedDistanceToPlayer(attackDistance) && playerInSight)
{
animator.SetBool("isRunning", false);
nav.isStopped = true;
StartCoroutine("AttackAnim");
}
if (!isOnSelectedDistanceToPlayer(attackDistance) && playerInSight)
{
Chase();
}
}
IEnumerator AttackAnim()
{
animator.SetTrigger("Attack");
yield return new WaitForSeconds(timeBetweenAttacks);
animator.ResetTrigger("Attack");
}
Relevant screenshots:
Assets that I used: https://assetstore.unity.com/packages/3d/characters/humanoids/zombie-30232
Also I checked and unchecked root motion few times. Baked into pose as well (except running, it cause some other error)
After a tilt he goes directly into object now:
Cube also has collider
transform.LookAt(enemyOfenemy); this line cause the problem you should use two axis x and z and keep y axis untouched.
Answer by icehex · Apr 29, 2020 at 06:58 PM
Like @Masterio identified, your character is trying to face the center of the position of the rectangular object, so as it gets closer it's going to point up towards that center point. You need it to continue pointing in the correct x-z direction, but not rotate around the Y axis. So you have to zero out the Y axis. Try:
transform.LookAt(Vector3.Scale(enemyOfenemy, new Vector3(1, 0, 1));
Vector3.Scale will multiply the two Vector3's element by element, so the resulting Vector3 that would be passed to transform.LookAt will be (enemyOfenemy.x, 0, enemyOfenemy.z).
Just to clarify, you have to zero out whichever axis corresponds to the red coordinate arrow in your image*. In my Unity red is the X-axis, so the new Vector3 would be Vector3(0, 1, 1). I think that can be changed so zero out the axis that makes sense for your project.
Thanks , I will try it out as soon as I can. Hopefully your answer is correct.
Totally worked. $$anonymous$$uch obliged for that. Thanks again
Your answer
Follow this Question
Related Questions
How can I make character assets walk a certain distance? 2 Answers
Only animate rotation not position 1 Answer
Mirrored animation doesnt rotate correctly 0 Answers
Run smooth continuous rotation animation of spinner using animator 1 Answer
Adding new Animation to existing Animator not working correctly 0 Answers