<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>자동포탑 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/%EC%9E%90%EB%8F%99%ED%8F%AC%ED%83%91/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Thu, 22 Dec 2022 06:11:26 +0000</lastBuildDate>
	<language>ko-KR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://lycos7560.com/wp-content/uploads/2022/11/cropped-cropped-cropped-log-1-150x150-1-80x80.png</url>
	<title>자동포탑 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Unity 자동 공격 포탑(Automatic Attack Turret) 구현</title>
		<link>https://lycos7560.com/unity/unity-auto-attack-turret/3533/</link>
					<comments>https://lycos7560.com/unity/unity-auto-attack-turret/3533/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Thu, 22 Dec 2022 06:04:53 +0000</pubDate>
				<category><![CDATA[Unity]]></category>
		<category><![CDATA[RND]]></category>
		<category><![CDATA[simulation]]></category>
		<category><![CDATA[stud]]></category>
		<category><![CDATA[turret]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[구현]]></category>
		<category><![CDATA[기능]]></category>
		<category><![CDATA[유니티]]></category>
		<category><![CDATA[자동포탑]]></category>
		<category><![CDATA[타겟팅]]></category>
		<category><![CDATA[터렛]]></category>
		<category><![CDATA[포탑]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=3533</guid>

					<description><![CDATA[<p>Unity(유니티)에서 자동 공격 포탑을 구현한 내용의 글입니다. (This is an article that implements an automatic attack turret in Unity turret.)</p>
<p>The post <a href="https://lycos7560.com/unity/unity-auto-attack-turret/3533/">Unity 자동 공격 포탑(Automatic Attack Turret) 구현</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:43px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Unity Version : 2021.3.5f1 </pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">나중에 모델과 이펙트까지 붙이면 될 것 같다.</p>



<p class="has-medium-font-size wp-block-paragraph">적에게 피해를 입히는 방법은 어떤식으로 구현하면 좋을지 생각해봐야 할 듯</p>



<p class="has-medium-font-size wp-block-paragraph">추가적으로 이 방법은 범위에 들어온 적 1기만 공격할 수 있지만 </p>



<p class="has-medium-font-size wp-block-paragraph">여러명을 공격하는 포탑에는 적용시킬 수 없을 듯하다. (Physics.OverlapSphere 사용을 생각 중)</p>



<div style="height:38px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-video"><video height="992" style="aspect-ratio: 1912 / 992;" width="1912" controls muted src="https://lycos7560.com/wp-content/uploads/녹화_2022_12_22_14_35_40_476.mp4"></video></figure>



<div style="height:77px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">사용한 방법은</p>



<p class="has-medium-font-size wp-block-paragraph">콜라이더를 이용하여 주변에 적을 식별(layer로 필터)하고 타겟팅합니다.</p>



<p class="has-medium-font-size wp-block-paragraph">물체와 자신과의 거리를 계산하여 특정 거리 이상으로 멀어지면 타겟팅을 해제하고 다시 대기모드로 변경합니다.</p>



<div style="height:34px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="719" height="572" src="https://lycos7560.com/wp-content/uploads/image-739.png" alt="" class="wp-image-3537" srcset="https://lycos7560.com/wp-content/uploads/image-739.png 719w, https://lycos7560.com/wp-content/uploads/image-739-300x239.png 300w" sizes="(max-width: 719px) 100vw, 719px" /></figure>



<p class="has-medium-font-size wp-block-paragraph">구현하면서 나온 문제는 중심거리는 멀어져서 타겟팅이 해제되지만 충돌범위에 속하여 오류가 발생합니다.</p>



<p class="has-medium-font-size wp-block-paragraph">-&gt; 해결 방법은 포탑의 탐색길이(파랑)와 타겟팅된 물체의 콜라이더 길이(노랑)의 합보다 중심거리가(빨강) 길면 해결됩니다.    </p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">뒤 생각없이 구현한 포탑 코드</h2>



<div style="height:32px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum eTurretState
{
    Nono = -1,
    idle,
    attack
}

public class turret : MonoBehaviour
{
    [SerializeField]
    public GameObject m_TurretTarget;

    [SerializeField]
    private eTurretState m_eTurretState = eTurretState.Nono;

    [SerializeField]
    public float m_AttackRange;
    
    [SerializeField]
    public float m_TurnSpeed;

    [SerializeField]
    private float m_AttackSpeed;

    [SerializeField]
    private float m_AttackCoolDown;

    public GameObject testHead;
    public GameObject RangeSqhere;


    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == 20)
        {
            this.m_TurretTarget = other.gameObject; // 터렛의 공격 대상 오브젝트에 추가
            // 공격 범위 콜라이더를 OFF;
            SphereColliderActive(false);
            StartCoroutine(this.TargettingCoroutine());
            this.m_eTurretState = eTurretState.attack;
        }

    }

    /// &lt;summary>
    /// 터렛의 적을 식별하는 콜라이더의 크기를 변경합니다.
    /// &lt;/summary>
    /// &lt;param name="state">&lt;/param>
    private void SphereColliderActive(bool state)
    {
        if (state) { this.RangeSqhere.transform.localScale = new Vector3(this.m_AttackRange * 2, 0.0001f, this.m_AttackRange * 2); }
        else { this.RangeSqhere.transform.localScale = new Vector3(0.001f, 0.0001f, 0.001f); }
    }

    /// &lt;summary>
    /// 터렛이 지속적으로 적을 타겟팅하는 코르틴
    /// &lt;/summary>
    /// &lt;returns>&lt;/returns>
    private IEnumerator TargettingCoroutine()
    {
        while (true)
        {
            // 만약 터렛의 타겟이 Null이거나 터렛과 
            if (m_TurretTarget == null || this.m_TurretTarget.gameObject.layer != 20 || Vector3.Distance(this.m_TurretTarget.transform.position, this.transform.position) > this.m_AttackRange + 2.0f)
            {
                this.m_eTurretState = eTurretState.idle;
                SphereColliderActive(true);
                break;
            }
            else
            {
                this.testHead.transform.rotation = Quaternion.Lerp(this.testHead.transform.rotation, Quaternion.LookRotation(this.m_TurretTarget.transform.position - this.transform.position), Time.deltaTime * this.m_TurnSpeed);
            }

            yield return null;
        }
    }

    public void SearchEnemy()
    {

    }



    public void AttackEnemy()
    {
        Debug.Log("Attack");
        //m_TurretTarget.GetComponent&lt;TestEnemy>().BeDamaged(this.Damage);

    }

    private void Start()
    {
        // 샘플

        this.RangeSqhere.transform.localScale = new Vector3(this.m_AttackRange * 2, 0.0001f, this.m_AttackRange * 2);

    }
    private void Update()
    {
        if (this.m_AttackCoolDown > 0f) {
            this.m_AttackCoolDown -= Time.deltaTime; }

        if (m_eTurretState == eTurretState.attack &amp;&amp; this.m_AttackCoolDown &lt;= 0f &amp;&amp; this.m_TurretTarget != null)
        {
            this.m_AttackCoolDown = this.m_AttackSpeed;
            AttackEnemy();
        }
    }
}
</pre>



<div style="height:42px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1222" height="1043" src="https://lycos7560.com/wp-content/uploads/image-740.png" alt="" class="wp-image-3539" srcset="https://lycos7560.com/wp-content/uploads/image-740.png 1222w, https://lycos7560.com/wp-content/uploads/image-740-300x256.png 300w, https://lycos7560.com/wp-content/uploads/image-740-768x656.png 768w" sizes="(max-width: 1222px) 100vw, 1222px" /></figure>



<div style="height:42px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="802" height="842" src="https://lycos7560.com/wp-content/uploads/image-741.png" alt="" class="wp-image-3540" srcset="https://lycos7560.com/wp-content/uploads/image-741.png 802w, https://lycos7560.com/wp-content/uploads/image-741-286x300.png 286w, https://lycos7560.com/wp-content/uploads/image-741-768x806.png 768w" sizes="(max-width: 802px) 100vw, 802px" /></figure>



<div style="height:32px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="789" height="899" src="https://lycos7560.com/wp-content/uploads/image-742.png" alt="" class="wp-image-3541" srcset="https://lycos7560.com/wp-content/uploads/image-742.png 789w, https://lycos7560.com/wp-content/uploads/image-742-263x300.png 263w, https://lycos7560.com/wp-content/uploads/image-742-768x875.png 768w" sizes="(max-width: 789px) 100vw, 789px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<p>The post <a href="https://lycos7560.com/unity/unity-auto-attack-turret/3533/">Unity 자동 공격 포탑(Automatic Attack Turret) 구현</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/unity/unity-auto-attack-turret/3533/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		<enclosure url="https://lycos7560.com/wp-content/uploads/녹화_2022_12_22_14_35_40_476.mp4" length="2936532" type="video/mp4" />

			</item>
	</channel>
</rss>
