Unity/Asset 분석

[RTS Engine] Attack 시 타겟을 향해 회전

lipnus 2021. 6. 21. 21:24
반응형

UnitMovement.cs

	private void UpdateIdleRotation ()
        {
            if (!canIdleRotate || nextRotationTarget == Quaternion.identity) //can the unit rotate when idle + there's a valid rotation target
                return;

            if (IdleLookAtTransform != null) //if there's a target object to look at.
                nextRotationTarget = RTSHelper.GetLookRotation(transform, IdleLookAtTransform.position, true); //keep updating the rotation target as the target object might keep changing position

            if (smoothIdleRotation)
                transform.rotation = Quaternion.Slerp(transform.rotation, nextRotationTarget, Time.deltaTime * idleAngularSpeed);
            else
                transform.rotation = nextRotationTarget;
        }

 

 

RTSHelper.cs

	public static Quaternion GetLookRotation(Transform transform, Vector3 targetPosition, bool reversed = false, bool fixYRotation = true)
        {
            if (reversed)
                targetPosition = transform.position - targetPosition;
            else
                targetPosition -= transform.position;

            if(fixYRotation == true)
                targetPosition.y = 0;
            if (targetPosition != Vector3.zero)
                return Quaternion.LookRotation(targetPosition);
            else
                return transform.rotation;
        }

reverse : 반대쪽을 향함

fixRotation : 위아래로도 방향에 맞추어서 회전 

반응형