Exemple #1
0
    protected void MoveTowardTarget()
    {
        // determine angular change based on speed and passed time
        var angularDistance = speedAngle * Time.deltaTime * speedMod;

        if (Math.Abs((targetAngle - positionAngle + 360) % 360) < angularDistance)
        {
            // if you have more movement than remaining distance, stop at the target position
            positionAngle = targetAngle;
            hasArrived    = true;
            myBrain.AnnounceArrival();
        }
        else
        {
            // determine movement direction based on smallest angle and new position angle
            if (Math.Abs(targetAngle - positionAngle) < 180)
            {
                positionAngle          = (positionAngle + Math.Sign(targetAngle - positionAngle) * angularDistance) % 360;
                mySpriteRenderer.flipX = Math.Sign(targetAngle - positionAngle) <= 0;
            }
            else
            {
                positionAngle          = (positionAngle - Math.Sign(targetAngle - positionAngle) * angularDistance + 360) % 360;
                mySpriteRenderer.flipX = Math.Sign(targetAngle - positionAngle) >= 0;
            }
        }

        // move creature sprite to positional angle
        var       angle = Math.PI * (positionAngle) / 180;
        Transform transform1;

        (transform1 = transform).localPosition = new Vector3(
            (float)(home.radius * Math.Cos(angle)),
            -(home.depth / 2) + layer * home.depth / home.noLayers,
            (float)(home.radius * Math.Sin(angle))
            );

        // adjust rotation
        transform1.localEulerAngles = new Vector3(90, 0, positionAngle + 90);
    }