Unity/Unity 리서치

Unity 포물선 발사체

lipnus 2023. 3. 4. 13:51
반응형

RTS Engine의 AttackObject 코드

    void Update()
    {
        if (isActive == false || effectObjComp.CurrentState != EffectObj.State.running) //if the attack object is inactive, do not move it
            return;

        //if there's a delay do not move the attack object
        if (delayTimer > 0.0f)
        {
            delayTimer -= Time.deltaTime; //delay timer
            if (delayTimer <= 0.0f) //when done
                transform.SetParent(null, true); //free attack object from parent
            return; //so that the attack object doesn't move while in delay
        }

        //if the movement attributes were not set, set them now
        if(mvtAttributesSet == false)
        {
            mvtAttributesSet = true;

            parabolicMvtEnabled = Vector3.Distance(transform.position, targetPosition) >= minDistance;
            mvtDirection = (targetPosition - transform.position).normalized;

            if (mvtType == MvtTypes.parabolic && parabolicMvtEnabled == true) //if this is a parabolic movement
            {
                nextPos = transform.position; //reset next position
                startTime = Time.time; //reset the parabolic mvt timer
                totalDistance = Vector3.Distance(targetPosition, transform.position);
                middlePosition = transform.position;
            }
            else //linear movement
                transform.rotation = Quaternion.LookRotation(mvtDirection);
        }


        //if the attack object can follow its target then update the target position and movement direction
        if(followTarget 
            && target != null
            && Vector3.Distance(target.transform.position, initialTargetPosition) <= followTargetDistance)
        {
            targetPosition = target.GetSelection().transform.position;
            mvtDirection = (targetPosition - transform.position).normalized;
        }

        if (mvtType == MvtTypes.linear || parabolicMvtEnabled == false)
        {
            transform.position += mvtDirection * speed * Time.deltaTime;
            lookAtPosition = targetPosition - transform.position;
        }
        else
        {
            //move the attack object on a parabola:
            transform.position = nextPos;

            System.Func<float, float> f = x => 4 * maxHeight * x * (-x + 1);
            middlePosition += mvtDirection * speed * Time.deltaTime;
            float currDistance = ((Time.time - startTime) * speed) / totalDistance;
            nextPos = new Vector3(middlePosition.x, f(currDistance) + middlePosition.y, middlePosition.z);

            lookAtPosition = nextPos - transform.position; //where the attack object will be looking at.
        }

        if (lookAtPosition != Vector3.zero)
            transform.rotation = Quaternion.LookRotation(lookAtPosition);
    }

실제로 발사되는 발사체에 Component로 붙어있는 코드

 

UML: https://drive.google.com/file/d/1gOr6raGyEks_x_hq6jsFP26Rak04RYk8/view?usp=sharing 

 

Google Drive: 로그인

이메일 또는 휴대전화

accounts.google.com

 

 

벽 뒤의 적 공격

Obstacle Mask를 Nothing으로 설정

반응형

'Unity > Unity 리서치' 카테고리의 다른 글

URP 쉐이더 종류  (0) 2023.04.21
DontDestroyOnLoad의 적용범위  (0) 2023.03.20
C# 확장매소드(Extension Method)  (0) 2023.02.16
gameObject에 SendMessage 보내기  (0) 2023.01.25
Enum을 활용한 Bit연산자 Flag  (0) 2023.01.24