魔法のメモ

CG N GAME BLOG

C#_Unity_逆引き01

Lerp

直線上にある2つのベクトル間を補間する関数

https://qiita.com/aimy-07/items/ad0d99191da21c0adbc3

 

DefaultExecutionOrder(-1)

スクリプトの実行順番をスクリプトから書く

【Unity】DefaultExecutionOrderでスクリプトの実行順番をスクリプトから書く - はなちるのマイノート

 

RequireComponent(typeof(NavMeshAgent))

コンポーネントを自動的にアタッチ

[Unity初心者Tips]確実に!必要なComponentを入れるRequire ComponentとReset() - Qiita

 

Instantiate

クローン生成

https://www.sejuku.net/blog/48180

 

★色を変える

void Update() { if (Input.GetKeyDown(KeyCode.R)) { GetComponent<Renderer> ().material.color = Color.red; }

 

★引数、戻り値など

int myInt = 5; void Start () { myInt = MultiplyByTwo(myInt); Debug.Log (myInt); } int MultiplyByTwo (int number) { int result; result = number * 2; return result; }

https://xr-hub.com/archives/8154

 

★if文

https://learn.unity.com/tutorial/if-statements?uv=2019.3&projectId=5c8920b4edbc2a113b6bc26a#5c8a31e4edbc2a00a9df2f46

 

■loop文

https://learn.unity.com/tutorial/loops-z2b?uv=2019.3&projectId=5c8920b4edbc2a113b6bc26a#5c8a402aedbc2a001f47ccc4

https://3dunity.org/unity-introduction/unity-csharp-programming/loop-sentence-for-while-foreach/

 

■Jump

f:id:heroroom:20211008084417p:plain

 

PS4のコントローラー

https://younaship.com/2019/01/29/unity%E3%81%A7ps4%E3%81%AE%E3%82%B3%E3%83%B3%E3%83%88%E3%83%AD%E3%83%BC%E3%83%A9%E3%83%BC%E3%82%92%E4%BD%BF%E3%81%86%E6%96%B9%E6%B3%95/

 

https://qiita.com/o_s_t/items/c18edeeee869d42c6eb9

 

■InvokeRepeating

x秒に関数を呼び出して、y秒ごとに繰り返し呼び出す

https://www.sejuku.net/blog/83762

 

■カメラ固定(横スクロールゲーム)

https://miyagame.net/yoko-scroll-camera/

 

■敵│移動

https://dkrevel.com/makegame-beginner/make-2d-action-enemy-move/

https://www.youtube.com/watch?v=NbA95f1FlXQ

f:id:heroroom:20211014095747p:plainf:id:heroroom:20211014095743p:plain

f:id:heroroom:20211014095745p:plain

f:id:heroroom:20211014095751p:plain

f:id:heroroom:20211014095753p:plain

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

public class Enemy : MonoBehaviour
{
    public float dirX;
    public float moveSpeed;
    public Rigidbody rb;
    public bool facingRight = false;
    public Vector3 localScale;

    // Start is called before the first frame update
    void Start()
    {
        localScale = transform.localScale;
        rb = GetComponent<Rigidbody>();
        dirX = -1f;
        moveSpeed = 3f;
    }

    private void OnTriggerEnter(Collider other)
    {
        if(other.GetComponent<WALLL>())
        {
            dirX *= -1f;
        }
    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector3(dirX * moveSpeed, rb.velocity.y);    
    }

    // Update is called once per frame
    void LateUpdate()
    {
        CheckWhereToFace();
    }

    void CheckWhereToFace()
    {
        if (dirX > 0)
            facingRight = true;
        else if (dirX < 0)
            facingRight = false;

        if (((facingRight) && (localScale.x <0)) || ((!facingRight) && (localScale.x > 0)))
            localScale.x *= -1;
        transform.localScale = localScale;
    }
}