Example #1
0
    void FixedUpdate()
    {
        if (disabled)
        {
            return;
        }

        if (doMove)
        {
            if (rotateToTarget == null)
            {
                movementDirection = (target.position - transform.position).normalized;
            }
            else
            {
                rotateToTarget.targetPos = target.position;
                rotateToTarget.RotateWithPhysics();
                movementDirection = transform.right;
            }

            Vector2 velocity = movementDirection * speed;
            if (rb.velocity != velocity)
            {
                rb.velocity = velocity;
            }
        }

        doMove = false;
    }
Example #2
0
    public override void Move()
    {
        if (disabled || target == null)
        {
            return;
        }

        Vector2 distanceToTarget = target.position - transform.position;

        bool targetReached = false;

        if (Mathf.Abs(distanceToTarget.x) < stopDistance.x && Mathf.Abs(distanceToTarget.y) < stopDistance.y)
        {
            targetReached = true;
        }

        if (targetReached)
        {
            movementDirection = Vector2.zero;
        }
        else
        {
            if (rotateToTarget == null)
            {
                movementDirection = distanceToTarget.normalized;
            }
            else
            {
                rotateToTarget.targetPos = target.position;
                rotateToTarget.RotateWithPhysics();
                movementDirection = transform.forward;
            }
        }
    }