Example #1
0
    public float MoveTo(Vector3 target, RectangleBound rectangleBound = null)
    {
        // no movement if pushed
        if (isPushed)
        {
            return(0);
        }

        float directionX = 0;
        bool  stopMoving = false;

        if (MoveUtils.TargetReachedXY(transform, target, speed, smoothTime))
        {
            // Target already reached
            stopMoving = true;
        }

        // Check if actor ist still in bounds
        if (!stopMoving && rectangleBound != null && !rectangleBound.IsInBoundX(transform.position))
        {
            stopMoving = true;
        }

        if (stopMoving)
        {
            return(0);
        }
        else if (target != Vector3.positiveInfinity)
        {
            if (transform.position.x > target.x)
            {
                directionX = -1;
            }
            if (transform.position.x < target.x)
            {
                directionX = 1;
            }
        }


        // perform moving action
        //targetPosition = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
        targetPosition = target - transform.position;
        targetPosition = targetPosition / targetPosition.magnitude * speed;
        if (moveSinus)
        {
            targetPosition = targetPosition + Vector3.up * (Mathf.Sin(Time.timeSinceLevelLoad * frequency) * magnitude);
        }
        return(directionX);
    }
Example #2
0
    // Moves GameObject in the direction of the target. Returns direction it is heading.
    public float MoveTo(Vector3 target, RectangleBound rectangleBound = null)
    {
        bool  stopMoving = false;
        float directionX = 0;

        // Check if actor ist still in bounds
        if (rectangleBound != null && !rectangleBound.IsInBoundX(transform.position))
        {
            stopMoving = true;
        }

        // check if target is reached
        if (MoveUtils.TargetReachedX(transform, target))
        {
            stopMoving = true;
        }

        if (stopMoving)
        {
            StopMoving();
        }
        else if (target != Vector3.positiveInfinity)
        {
            if (transform.position.x > target.x)
            {
                directionX = -1;
            }
            if (transform.position.x < target.x)
            {
                directionX = 1;
            }
        }

        OnMove(directionX, 0F);
        return(directionX);
    }