::目標まで移動してヒットするのを作ってみよう。

:2020-10-28

 

youtu.be/-pV261uo324

>>たくさん、コメントを入れよう!(自分及び相手が理解しやすいように!)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class App : MonoBehaviour
{
    // :: Variables
    List<GameObject> unitz;

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Hello Start");

        // :: Initialise
        unitz = new List<GameObject>();

        // :: Load Resource
        var prefab = Resources.Load<GameObject>("Prefabs/ch_03_01");

        // :: Render and Set Position with prefab : Character A
        unitz.Add(Object.Instantiate(prefab));
        unitz[0].transform.position = new Vector3(0.5f, 0, 0);
        unitz[0].transform.eulerAngles = new Vector3(0, -90, 0);
        unitz[0].AddComponent<Hero>();

        // :: Render and Set Position with prefab : Character B
        unitz.Add(Object.Instantiate(prefab));
        unitz[1].transform.position = new Vector3(-0.5f, 0, 0);
        unitz[1].transform.eulerAngles = new Vector3(0, 90, 0);
        unitz[1].AddComponent<Hero>();

        // :: Initialise Button Continuous Attack
        Button btnContinuous = GameObject.Find("ButtonA").GetComponent<Button>();
        btnContinuous.onClick.AddListener(() =>
        {
            // :: Start Attack
            unitz[0].GetComponent<Hero>().StartAttack();
        });

        // :: Initialise Button Go target and attack
        Button btnAttack = GameObject.Find("ButtonB").GetComponent<Button>();
        btnAttack.onClick.AddListener(() =>
        {
            // :: Start Attack
            unitz[0].GetComponent<Hero>().GoTargetAndAttack(unitz[1]);
        });

        // :: Initialise Button Get Back
        Button btnGetBack = GameObject.Find("ButtonC").GetComponent<Button>();
        btnGetBack.onClick.AddListener(() =>
        {
            // :: Get Back Base Position
            unitz[0].GetComponent<Hero>().GetBack();
        });

    }

    // Update is called once per frame
    void Update()
    {
    }
}
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hero : MonoBehaviour
{
    // Start is called before the first frame update
    Vector3 basePosition;
    void Start()
    {
        // :: Initialise
        deadlineAttackTime = new float[continuousAttack.Length];
        for (int i = 0; i < continuousAttack.Length; i++)
        {
            // :: Remember Deadline Length in variable
            Animation anim = this.gameObject.GetComponent<Animation>();
            deadlineAttackTime[i] = anim[continuousAttack[i]].length;
        }
        // :: Remember Base Position
        basePosition = this.gameObject.transform.position;
    }

    // :: Get Back position
    public void GetBack()
    {
        // :: Reset Position
        this.gameObject.transform.position = basePosition;

        // :: Reset Target
        target = null;
        isNearTheTarget = false;
    }
    
    // :: Start Continuous Attack
    public void StartAttack()
    {
        // :: Start Update
        checkContinuousAttack = true;

        // :: Initialise
        continuousIndex = 1;
        elapsedTime = 0;
        this.didHit = false;
    }

    // :: Go There
    bool checkGo = false; // :: Check Go
    GameObject target; // :: Target
    bool isNearTheTarget = false; // :: Is near the Target;

    // :: Go Target And Attack
    public void GoTargetAndAttack(GameObject target)
    {
        // :: Input Target
        this.target = target;

        // :: Go
        checkGo = true;

        // :: Run Loop
        this.gameObject.GetComponent<Animation>().Play("run@loop");
    }

    bool checkContinuousAttack = false; // :: Check Continuous Attack;
    bool didHit; // :: Check Hit
    private int continuousIndex; // :: Continuous Attack Index
    private float[] hitTimeForContinuousAttack = { 0f, 0.34f, 0.5f, 0.5f };
    private string[] continuousAttack = { "idle@loop", "attack_sword_01", "attack_sword_02", "attack_sword_03" }; // :: Continuous Attack List
    private float[] deadlineAttackTime; // :: Deadline Lengths
    private float elapsedTime; // :: Elapsed Time

    // Update is called once per frame
    void Update()
    {        // :: Check Continuous is True
        if (this.checkContinuousAttack)
        {
            // :: Play Animation
            this.gameObject.GetComponent<Animation>().Play(continuousAttack[continuousIndex]);
            // :: Elapsed Time
            elapsedTime += Time.deltaTime;
            Debug.Log(elapsedTime);

            // :: When Near the Target & hitTiming
            if(this.isNearTheTarget == true && elapsedTime >= hitTimeForContinuousAttack[continuousIndex] && this.didHit == false)
            {
                // :: When hitTime is Exist
                if(hitTimeForContinuousAttack[continuousIndex] > 0)
                {
                    Debug.LogFormat("Hit! : {0}", continuousIndex);
                    Debug.Log(hitTimeForContinuousAttack[continuousIndex]);

                    // :: Animation Reset
                    target.GetComponent<Animation>().Rewind();

                    // :: Hit Animation
                    target.GetComponent<Animation>().Play("damage");

                    // :: Reset didHit
                    this.didHit = true;
                }
            }

            // :: If elapsedTime is bigger than deadlineAttackTime
            if (elapsedTime >= deadlineAttackTime[continuousIndex])
            {
                // :: ElapsedTime Reset
                elapsedTime = 0;

                // :: Next Index
                continuousIndex += 1;

                // :: If Continuous Index is Out of Range
                if (continuousIndex >= continuousAttack.Length)
                {
                    // :: Continuous Index Reset : idle@loop
                    continuousIndex = 0;

                    // :: Don't more
                    checkContinuousAttack = false;
                }

                // :: Show Next Animation;
                this.gameObject.GetComponent<Animation>().Play(continuousAttack[continuousIndex]);

                // :: didHit Reset
                this.didHit = false;
            }
        }

        // ::: Check Going
        if(this.checkGo)
        {
            // :: Check Target Distance
            float distance = Vector3.Distance(this.gameObject.transform.position, this.target.transform.position);
            // ::: when distance is near
            if(distance <= 0.35f)
            {
                // ::: Stop and Attack;
                this.checkGo = false;
                this.StartAttack();
                this.isNearTheTarget = true;
            }
            // :: Move Toward
            this.transform.position = Vector3.MoveTowards(this.transform.position, this.target.transform.position, 0.1f * Time.deltaTime);
        }
    }
}

>> 2nd Version : Vector3.MoveTowardsを使わずにTranslateでやってみよう。 

using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hero : MonoBehaviour
{
    // Start is called before the first frame update
    Vector3 basePosition;
    void Start()
    {
        // :: Initialise
        deadlineAttackTime = new float[continuousAttack.Length];
        for (int i = 0; i < continuousAttack.Length; i++)
        {
            // :: Remember Deadline Length in variable
            Animation anim = this.gameObject.GetComponent<Animation>();
            deadlineAttackTime[i] = anim[continuousAttack[i]].length;
        }
        // :: Remember Base Position
        basePosition = this.gameObject.transform.position;
    }

    // :: Get Back position
    public void GetBack()
    {
        // :: Reset Position
        this.gameObject.transform.position = basePosition;

        // :: Reset Target
        target = null;
        isNearTheTarget = false;
    }
    
    // :: Start Continuous Attack
    public void StartAttack()
    {
        // :: Start Update
        checkContinuousAttack = true;

        // :: Initialise
        continuousIndex = 1;
        elapsedTime = 0;
        this.didHit = false;
    }

    // :: Go There
    bool checkGo = false; // :: Check Go
    GameObject target; // :: Target
    bool isNearTheTarget = false; // :: Is near the Target;

    // :: Go Target And Attack
    public void GoTargetAndAttack(GameObject target)
    {
        // :: Input Target
        this.target = target;

        // :: Look at Target
        this.transform.LookAt(target.transform);

        // :: Go
        checkGo = true;

        // :: Run Loop
        this.gameObject.GetComponent<Animation>().Play("run@loop");
    }

    bool checkContinuousAttack = false; // :: Check Continuous Attack;
    bool didHit; // :: Check Hit
    private int continuousIndex; // :: Continuous Attack Index
    private float[] hitTimeForContinuousAttack = { 0f, 0.34f, 0.5f, 0.5f };
    private string[] continuousAttack = { "idle@loop", "attack_sword_01", "attack_sword_02", "attack_sword_03" }; // :: Continuous Attack List
    private float[] deadlineAttackTime; // :: Deadline Lengths
    private float elapsedTime; // :: Elapsed Time

    // Update is called once per frame
    void Update()
    {        // :: Check Continuous is True
        if (this.checkContinuousAttack)
        {
            // :: Play Animation
            this.gameObject.GetComponent<Animation>().Play(continuousAttack[continuousIndex]);
            // :: Elapsed Time
            elapsedTime += Time.deltaTime;
            Debug.Log(elapsedTime);

            // :: When Near the Target & hitTiming
            if(this.isNearTheTarget == true && elapsedTime >= hitTimeForContinuousAttack[continuousIndex] && this.didHit == false)
            {
                // :: When hitTime is Exist
                if(hitTimeForContinuousAttack[continuousIndex] > 0)
                {
                    Debug.LogFormat("Hit! : {0}", continuousIndex);
                    Debug.Log(hitTimeForContinuousAttack[continuousIndex]);

                    // :: Animation Reset
                    target.GetComponent<Animation>().Rewind();

                    // :: Hit Animation
                    target.transform.LookAt(this.transform);
                    target.GetComponent<Animation>().Play("damage");

                    // :: Reset didHit
                    this.didHit = true;
                }
            }

            // :: If elapsedTime is bigger than deadlineAttackTime
            if (elapsedTime >= deadlineAttackTime[continuousIndex])
            {
                // :: ElapsedTime Reset
                elapsedTime = 0;

                // :: Next Index
                continuousIndex += 1;

                // :: If Continuous Index is Out of Range
                if (continuousIndex >= continuousAttack.Length)
                {
                    // :: Continuous Index Reset : idle@loop
                    continuousIndex = 0;

                    // :: Don't more
                    checkContinuousAttack = false;
                }

                // :: Show Next Animation;
                this.gameObject.GetComponent<Animation>().Play(continuousAttack[continuousIndex]);

                // :: didHit Reset
                this.didHit = false;
            }
        }

        // ::: Check Going
        if(this.checkGo)
        {
            // :: Check Target Distance
            float distance = Vector3.Distance(this.gameObject.transform.position, this.target.transform.position);
            Debug.Log(distance);
            // ::: when distance is near
            if(distance <= 0.35f)
            {
                // ::: Stop and Attack;
                this.checkGo = false;
                this.StartAttack();
                this.isNearTheTarget = true;
            }

            // :: Move There with Translate
            this.transform.Translate(this.transform.forward * 0.1f * Time.deltaTime, Space.World);

            // :: Move Toward
            //this.transform.position = Vector3.MoveTowards(this.transform.position, this.target.transform.position, 0.1f * Time.deltaTime);
        }
    }
}
블로그 이미지

RIsN

,