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

: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

,

《文法》

【Nullじゃないなら実行】

// :: Callback
Callback_CompletedFX?.Invoke();
// :: Same the above
/*if (Callback_CompletedFX != null)
{
	Callback_CompletedFX();
}*/

《EASY to USE》

【EnumのDescriptionを取得して使用】

// :: Enums : Animation Key
private enum eAnimation
{
  [Description("idle@loop")]
  IDLE,
  [Description("run@loop")]
  RUN,
  [Description("walk@loop")]
  WALK
}

// :: for use enum Description
// ::: Ref : http://kawakawa2000.jugem.jp/?eid=27
// ::: I don't understand perfectly yet.
private string GetEnumDescription(eAnimation value)
{
	FieldInfo fi = value.GetType().GetField(value.ToString());
	var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

	var descriptionString = attributes.Select(ele => ele.Description).FirstOrDefault();

	if(descriptionString != null)
	{
		return descriptionString;
	}
	return value.ToString();
}

void Update() 
{
	anim.Play(this.GetEnumDescription(eAnimation.RUN));
}

【Unity Editorにツールを登録

// :: for Using
using UnityEditor;

// :: Easy to use
public class MenuTest : MonoBehaviour
{
    // :: Location
    [MenuItem("MyMenu/Delete All PlayerPrefs")]
    // :: Program : Delete All PlayerPrefs
    static void DeleteAllPlayerPrefs()
    {
        PlayerPrefs.DeleteAll();
        Debug.Log("deleted all player prefs");
    }
}

《イベント》

【ボタンにイベントリスナーを追加】

>>クリックすると動くように

// :: Add Event Listener
btn.onClick.AddListener(this.ClickedButton);

【数秒後に実行】
>>Singletonに定義してそれを持ってきて使うのも可能

<<CT_Ruler>>
// :: Do Action after Waiting
UIController.WaitForSecondsAndDo(10f, (() => { UIController.FadeIn(GOHolder.SPRITE_handRight); }));

<<Sing_UIController>>
// :: Do Action after Waiting
public void WaitForSecondsAndDo(float time, System.Action action)
{
	this.StartCoroutine(WaitForSecondsAndDoImplement(time, action));
}
private IEnumerator WaitForSecondsAndDoImplement(float time, System.Action action)
{
	yield return new WaitForSeconds(time);
	action();
}

【ゲーム停止】

// :: Pause Game
Time.timeScale = 0;
// :: RePlay Game
Time.timeScale = 1;

《出力》

【PrefabをベースにGameObjectの複製を作ってそれを画面に出力】

// :: Make GameObject & Render
GameObject prefab =  Resources.Load<GameObject>("ch_03_01");
GameObject clone =  Instantiate<GameObject>(prefab);

【出力する際に親(Parent)と位置を設定】

// :: Render Chest Closed and Set Position
// ::: Use Transform Parent
// ::: 1. GameObject
// ::: 2. Position
// ::: 3. Rotation
// ::: 4. Parent transform
chestClosed = Object.Instantiate<GameObject>(chestPrefabA, // 1
            lastTarget.transform.position + new Vector3(0, 0, 0.5f), // 2 
            Quaternion.Euler(-90, -90, 0), // 3
            lastTarget.transform); // 4

【出力してから親(Parent)を設定】

// :: Create Weapon and Set Parent
GameObject spear = Object.Instantiate<GameObject>(Resources.Load<GameObject>("Spear_7"));
spear.transform.SetParent(dummyRHand);
// :: Change Position
spear.transform.localPosition = new Vector3(0, 0, 0);
spear.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, 0));

【画面にいるGameObjectの出力を消す】

// :: Destroy Game Object
Destroy(itm);

【3Dのキャラ位置に合わせて2DキャンバスにHUDを出力】

// :: When Button Clicked : Show HUD
this.BUTTON_test.onClick.AddListener(() =>
{
	// :: Instantiate with Canvas
	var go = Instantiate<GameObject>(this.hudPrefab, this.canvas.transform);

	// :: Check Where do you want set the position
	// ::: Main Camera World Point => Screen Point
	var screenPoint = Camera.main.WorldToScreenPoint(playerGO.transform.position);

	// ::: Screen Point => Local(Canvas) Point
	Vector2 localPoint;
	// ::: ((RectTransform) Canvas Transform, Screen Point, Camera which you will set HUD, out local Point)
	RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)canvas.transform, screenPoint, this.uiCam, out localPoint);

	// :: Change HUD Local position
	go.transform.localPosition = localPoint;

	// :: Play and Destroy
	var hud = go.GetComponent<HUDText>();
	hud.Init(992);
	hud.Play();
	hud.Callback_Play = () => {
		Destroy(hud.gameObject);
	};
});

《方向と移動》

【1歩前に動く:マップ基準】

// :: Map Position
hero.transform.position += Vector3.forward;
//this.gameObject.transform.Translate(Vector3.forward, Space.Self);

1歩前に動く:自分基準】

// :: Self Position
hero.transform.position += hero.transform.forward;
//this.gameObject.transform.Translate(this.transform.forward, Space.World);

【目標の方向を見るように角度を修正】

// :: Look target Direction
Vector3 target = new Vector3(1, 0.49f, -2) - itm.transform.position;
itm.transform.rotation = Quaternion.LookRotation(target);

【目標を見てその方向に走る(結果:目標を通って走る)】

// :: Look at Target
this.transform.LookAt(target.transform);
// :: 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);

《衝突》

【衝突無視】

// :: Find all BoxCollider2D
foreach (var itm in GameObject.FindObjectsOfType<BoxCollider2D>())
{
	// :: Player : Ignore it
	Physics2D.IgnoreCollision(playerRigidbody.gameObject.GetComponent<CircleCollider2D>(), itm, true);
}

【RayCastで衝突感知】

// :: Hit Checker
RaycastHit hit;
// :: When Racast Hit
// ::: (Start Position, Direction, Hit Object, Max Distance)
if (Physics.Raycast(this.player.transform.position, this.player.transform.forward, out hit, 1f))
{
	// :: Check Hit Object's Name
	Debug.Log(hit.transform.gameObject.name);
	// :: Show ray for Debug
	Debug.DrawRay(this.player.transform.position, this.player.transform.forward * 1f, Color.red);
}

《SCENE移動》

【SCENE移動時に完了をチェック、そしてその後に何かを実行】

// :: Load Scene with Done Checker
UnityEngine.AsyncOperation async = SceneManager.LoadSceneAsync(this.GetEnumDescription(sceneType));
// :: When Scene Load Completed
async.completed += Async_completed;

// :: Do Action
private void Async_completed(UnityEngine.AsyncOperation obj)
{
	if(obj.isDone)
	{
    	Debug.Log("Load Completed");
	}
}

 

블로그 이미지

RIsN

,

【キャンバス】

・他のシーンにキャンバス丸ごと複製するとタッチが動かない。

⇒キャンバスは各シーンの自体のものを使おう。

【アニメーション】

・アニメーション(Damage)が終了するまで同じアニメーション(Damage)が動かない時の方法。

⇒!Animation.Rewind()!//初期化

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

【ビルド問題】

・JSONを読む時に避けるべきの文法

File.ReadAllText({filePath})

:Androidにビルドするとファイルの位置が変更されるので、JSONファイルなどを読めない場合がある。

 

⇒!こちらを使おう!

Resources.Load<TextAsset>({filePath})

:参考サイト:docs.unity3d.com/ScriptReference/Resources.Load.html

 

Unity - Scripting API: Resources.Load

If an asset can be found at path, it is returned with type T, otherwise returns null. If the file at path is of a type that cannot be converted to T, also returns null. The path is relative to any folder named Resources inside the Assets folder of your pro

docs.unity3d.com

:まだ意味は完全に理解していないが、こちらはAndroidビルドをしても関係なく動いた!

블로그 이미지

RIsN

,