场景控制-死亡线
-
创建Empty,命名为DeadLine,添加Box Collider 2D组件,并调整好位置大小,选中Is Trigger,并设置tag为DeadLine
-
给Player添加一个dead音效
-
在PlayerController.cs中,修改代码:
private void OnTriggerEnter2D(Collider2D collision) { //收集物品 if (collision.tag == "Collection") { cherryAudio.Play(); Destroy(collision.gameObject); cherry += 1; CherryNum.text = cherry.ToString(); } //碰撞DeadLine if (collision.tag == "DeadLine") { GetComponent<AudioSource>().enabled = false;//关闭第一个音频 deadAudio.Play();//播放死亡音效 Invoke("Restart",1.5f);//延迟触发Restart方法 } } void Restart() { //碰撞DeadLine SceneManager.LoadScene(SceneManager.GetActiveScene().name); }
场景控制-场景切换
-
创建“EnterHouse.cs”,添加代码:
void Update() { //按下E切换场景 if (Input.GetKeyDown(KeyCode.E))//key按下,bottom按住 { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1); //buildIndex编号 } }
场景编号可在File -> Build Settings中将场景拖进Scenes in Build中查看
将脚本拖拽进EnterDialog对象中,即可实现场景切换
代码优化
-
删除idle有关代码以及Animator窗口中相关属性
-
贴图有蓝线:Grid的Inspector窗口中的Grid中的Cell Size的XY改为0.99
-
跳跃下蹲手感问题:
void FixedUpdate() { //有关Time.FixedDeltaTime的代码放在FixedUpdate中 if(!isHurt) { Movement();//Movement中都使用Time.FixedDeltaTime } SwitchAnim(); } private void Update() { //在Update中调用这两个方法,在FixedUpdate中调用Movement方法 //在Movement方法中删除跳跃和下蹲代码 Jump(); Crouch(); } void Jump() { //角色跳跃 if (Input.GetButton("Jump") && coll.IsTouchingLayers(ground)) { //注意使用GetButtom rb.velocity = new Vector2(rb.velocity.x, jumpForce * Time.fixedDeltaTime); jumpAudio.Play(); anim.SetBool("jumping", true); } }
视觉差Parallax
-
将BackGround的Collider移动至新的Object,命名为Area
-
新建Script,命名为Parallax.cs,添加代码:
public Transform cam; public float moveRate; private float startPointX,startPointY; public bool lockY; // Start is called before the first frame update void Start() { startPointX = transform.position.x; startPointY = transform.position.y; } // Update is called once per frame void Update() { if (lockY) { transform.position = new Vector2(startPointX + cam.position.x * moveRate, transform.position.y); } else { transform.position = new Vector2(startPointX + cam.position.x * moveRate, startPointY + cam.position.y * moveRate); } }
-
将Parallax.cs拖拽进BackGround,将MainCamera拖拽进Cam,并修改Move Rate,并选择是否lockY