Combat / Enemy State Lesson : slimes are sometimes spinning when moving

So for the Enemy State lesson of the Combat course section, I have the slimes moving around, but every once in awhile they will start spinning as well as moving. I followed the instructions in the lesson and have the same code so not sure why this is happening.

Enemy AI class

    private IEnumerator RoamingRouting()
    {
        while (_state == State.Roaming)
        {
            var roamPos = GetRoamingPosition();
            _enemyPathfinding.MoveTo(roamPos);
            yield return new WaitForSeconds(2f);
        }
    }

    private Vector2 GetRoamingPosition()
    {
        return new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f)).normalized;
    }

EnemyPathfinding Class
    private void FixedUpdate()
    {
        Move();
    }

    private void Move()
    {
        _rigidbody.MovePosition(_rigidbody.position + _moveDir * (_moveSpeed * Time.fixedDeltaTime));
    }

    public void MoveTo(Vector2 targetPos)
    {
        _moveDir = targetPos;
    }

What is causing the spin to happen?

I suspect this is the Rigidbody2D rotating it. Make sure in your Rigidbody2D that Freeze Rotation Z is checked in the Constraints foldout.
image

Yes, that was it, thanks. I knew I was missing something, just couldn’t put my finger on it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms