모름

구현모습

 

 

위 GIF에서처럼 총알이 Fade로 사라지는 효과 및 적이 빨간색으로 변하며 공격하는 효과를 아래 공식으로 구현이 가능합니다. 물론 응용할 여지도 많습니다.

 

IEnumerator Fade() {
    yield return new WaitForSeconds(lifeTime);

    float percent = 0;
    float speed = 1 / fadeTime;

    Material mat = GetComponent<Renderer>().material;
    Color initialColour = mat.color;

    while (percent < 1) {
        percent += Time.deltaTime * speed;
        mat.color = Color.Lerp(initialColour, Color.clear, percent);
        yield return null;
    }

    Destroy(gameObject);
}

 

대략 위 코드와 같은 느낌으로 사용이 가능합니다. 튜토리얼을 따라하는 중인데, 위와같은 유형이 반복적으로 등장하여 정리합니다. 위 코드는 percent를 speed 변수에 맞추어 1까지 채워주고, 이를 Color.Lerp를 인자에 percent를 넣어 Fade를 pecent가 채워지는 속도에 맞춰주고있습니다.

 

IEnumerator Attack()
{
    currentState = State.Attacking;
    pathfinder.enabled = false;

    Vector3 originalPosition = transform.position;
    Vector3 dirToTarget = (target.position - transform.position).normalized;
    Vector3 attackPosition = target.position - dirToTarget * myCollisionRadius;
    //Vector3 attackPosition = target.position;

    float attackSpeed = 3;
    float percent = 0;

    skinMaterial.color = Color.red;
    bool hasAppliedDamage = false;

    while (percent <= 1)
    {
        if (percent >= 0.5f && !hasAppliedDamage) {
            hasAppliedDamage = true;
            targetEntity.TakeDamage(damage);
        }

        percent += Time.deltaTime * attackSpeed;
        float interpolation = (-Mathf.Pow(percent, 2) + percent) * 4;
        transform.position = Vector3.Lerp(originalPosition, attackPosition, interpolation);

        yield return null;
    }

    skinMaterial.color = originalColor;
    currentState = State.Chasing;
    pathfinder.enabled = true;
}

 

이를 응용하여 공격 애니메이션에 응용도 가능합니다. percent에 따라서 앞으로 갔다가 뒤로가는것입니다. Vector3.Lerp에 percent 변수를 이용하는데, 여기선 대칭함수 공식 추가로 이용하여 찌르기 효과를 구현합니다.