调试背景
2D游戏的制作最重要的是层的设置
先设置两个层①Layers②Edit Layers③Sorting Layers添加两个层BackGround和Front
调试大雁
创建2D object改名BackGround设置背景图片修改为BackGround 0层
创建2D object添加大雁图片Ctrl+6,保存,把大雁动态模型全选,插入。调整时间可以看起来舒服,调整所在层为BackGround 1层
添加脚本使大雁从屏幕右边开始飞入左边飞出
publicclassSwanFly : MonoBehaviour {
//天鹅起飞的位置
privateVector3 startPosition;
publicfloat speed;
// Use this for initialization
void Start () {
Debug.Log("屏幕的宽度" + Screen.width);
Debug.Log("屏幕的高度" + Screen.height);
Vector3 screenSize = Camera.main.ScreenToWorldPoint(newVector3(Screen.width, Screen.height, 0));
float swanExtentsX = transform.GetComponent<Renderer>().bounds.extents.x;
startPosition = newVector3(screenSize.x + transform.position.y, transform.position.z);
transform.position = startPosition;
}
// Update is called once per frame
void Update () {
if (transform.position.x <-startPosition.x)
{
transform.position = startPosition;
}
transform.Translate(Vector3.right * -1 * speed * Time.deltaTime);
}
}
调整草地
设置空物体,在其中创建草地,两边超过相机边框。
在空物体上挂载BoxCollider2D组件,点击EditCollider 会有一个绿色方框调整位置
如图所示即可
游戏对象设置
设置球添加Front0层,挂载Circle Collider2D和Rigibody2D组件
创建一个2D空物体挂载脚本,随机产生球
publicclassCreatBall : MonoBehaviour {
//保龄球预制体
publicGameObject ballPrefab;
//产生的范围
privatefloat createRange;
//产生的时间
publicfloat rate;
// Use this for initialization
void Start () {
Vector3 screenSize = Camera.main.ScreenToWorldPoint(newVector3(Screen.width, Screen.height, 0));
float ballExtentsX = ballPrefab.transform.GetComponent<Renderer>().bounds.extents.x;
createRange = screenSize.x -ballExtentsX;
}
// Update is called once per frame
void Update () {
rate -= Time.deltaTime;
if (rate<0)
{
Vector3 position = newVector3(Random.Range(-createRange, createRange),transform.position.y, transform.position.z);
GameObject ball = Instantiate(ballPrefab, position, Quaternion.identity) asGameObject;
//下一个保龄球产生的时间
rate = Random.Range(1.5f, 2.5f);
Destroy(ball, 3f);
}
}
}
调试接物体的物体
创建两个2D object分别添加帽子的内沿和外沿,外沿为内沿的子物体
内沿层Front 0层 外延层Front 1层
分别挂载Edge Collider 2D组件,内沿勾选Is Trigger,发生碰撞
设置Edit Collider
挂载脚本接球然后销毁产生粒子效果
publicclassHatController : MonoBehaviour {
//帽子移动的范围
privatefloat HatMoveRange;
//粒子产生的位置
publicGameObject particleSystemPoint;
publicGameObject particle;
// Use this for initialization
void Start () {
Vector3 screenSize = Camera.main.ScreenToWorldPoint(newVector3(Screen.width, Screen.height, 0));
float hatExtentsX = transform.GetComponent<Renderer>().bounds.extents.x;
HatMoveRange = screenSize.x -hatExtentsX;
}
// Update is called once per frame
void Update () {
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float x = Mathf.Clamp(mousePosition.x,-HatMoveRange, HatMoveRange);
transform.position = newVector3(x, transform.position.y, transform.position.z);
}
voidOnTriggerEnter2D(Collider2D col)
{
//粒子产生
GameObject tempParticle = Instantiate(particle,particleSystemPoint.transform.position, Quaternion.identity)asGameObject;
Destroy(col.gameObject);
Destroy(tempParticle, 2f);
}
}
设置粒子效果
设置预制体的Renderer中 Sorting Layer为Front 2层
离子产生的位置设置一个2D空物体成为帽子内沿的子物体,调整位置