Unity-Roguelike笔记

游戏元素

GameObject Tag Layer SortingLayer Rigidbody 2D Box Collider 2D
Player Player BlockingLayer Units Kinematic Size(0.9,0.9)
Enemy Enemy BlockingLayer Units Kinematic
Floor Untagged Default Floor / /
OuterWall Untagged BlockingLayer Floor /
Wall Untagged BlockingLayer Items /
Food Food Default Items / Is Trigger
Exit Exit Default Items / Is Trigger

角色移动

移动动作

protected IEnumerator SmoothMovement (Vector3 end)
{
    float sqrRemainingDis = (transform.position - end).sqrMagnitude;
    while (sqrRemainingDis > float.Epsilon) {
        Vector3 newPos = Vector3.MoveTowards (rb2D.position, end, inverseMoveTime * Time.deltaTime);
        rb2D.MovePosition (newPos);
        sqrRemainingDis = (transform.position - end).sqrMagnitude;

        yield return null;
    }
}

StartCoroutine (SmoothMovement (end));

使用协程达到Update的效果。

移动障碍物判断

//避免射线与自身的碰撞器发生碰撞
boxcollider.enabled = false;
hit = Physics2D.Linecast (start, end, blockingLayer);
boxcollider.enabled = true;
return (hit.transform == null)

游戏管理

单例对象GameManger类创建

public static GameManager instance = null;

void Awake ()
{
    if (instance == null) {
        instance = this;
    } else if (instance != this) {
        Destroy (gameObject);
    }

    DontDestroyOnLoad (gameObject);
}

场景管理

//场景的重新载入
SceneManager.LoadScene (0);

// 添加委托当场景加载完后通知
SceneManager.sceneLoaded += OnSceneLoaded;
void OnSceneLoad (Scene scene, LoadSceneMode sceneMode){}

音效管理

//使用 params 关键字可以指定采用数目可变的参数的方法参数。
public void RandomizeSfx (params AudioClip[] clips)