-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
public class AudioManager : MonoBehaviour | ||
{ | ||
public static AudioManager Instance; | ||
|
||
private AudioSource player; | ||
|
||
public AudioClip jump; | ||
public AudioClip die1; | ||
public AudioClip die2; | ||
|
||
// Start is called before the first frame update | ||
void Start() | ||
{ | ||
Instance = this; | ||
player = GetComponent<AudioSource>(); | ||
} | ||
|
||
//播放跳跃音效 | ||
public void PlayJump() | ||
{ | ||
player.PlayOneShot(jump); | ||
} | ||
|
||
//播放死亡音效 | ||
public void PlayDie() | ||
{ | ||
//BGM停止 | ||
player.Stop(); | ||
//播放死亡音效 | ||
player.PlayOneShot(die1); | ||
//1s后调用PlayDie2 | ||
Invoke("PlayDie2", 1f); | ||
} | ||
|
||
//后续音效 | ||
void PlayDie2() | ||
{ | ||
player.PlayOneShot(die2); | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using UnityEngine.Rendering; | ||
|
||
public class BgControl : MonoBehaviour | ||
{ | ||
private float offset = 0; | ||
|
||
// Start is called before the first frame update | ||
void Start() | ||
{ | ||
|
||
} | ||
public void move(float dir) | ||
{ | ||
if(dir >0 ) | ||
{ | ||
GetComponent<Renderer>().material.SetTextureOffset("_MainTex", new Vector2(offset += 0.3f * Time.deltaTime, 0)); | ||
} | ||
else | ||
{ | ||
GetComponent<Renderer>().material.SetTextureOffset("_MainTex", new Vector2(offset -= 0.3f * Time.deltaTime, 0)); | ||
} | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
public class FireControl : MonoBehaviour | ||
{ | ||
private PlayerControl pc; | ||
|
||
void Start() | ||
{ | ||
pc = GameObject.FindWithTag("Player").GetComponent<PlayerControl>(); | ||
|
||
} | ||
|
||
void Update() | ||
{ | ||
//如果玩家死亡 | ||
if(pc.hp < 1) | ||
{ return; } | ||
//如果出屏幕,销毁 | ||
if(transform.position.x < -3f) | ||
{ | ||
Destroy(gameObject); | ||
} | ||
//移动 | ||
transform.Translate(Vector3.left * Time.deltaTime * 1f); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
public class FireManager : MonoBehaviour | ||
{ | ||
|
||
public GameObject Fire; | ||
|
||
//间隔时间 | ||
private float cd = 2f; | ||
|
||
private PlayerControl pc; | ||
void Start() | ||
{ | ||
pc = GameObject.FindWithTag("Player").GetComponent<PlayerControl>(); | ||
} | ||
|
||
// Update is called once per frame | ||
void Update() | ||
{ | ||
if(pc.hp < 1) | ||
{ | ||
return; | ||
} | ||
cd -= Time.deltaTime; | ||
if(cd <= 0) | ||
{ | ||
//创建火圈 | ||
Instantiate(Fire, transform.position, Quaternion.identity); | ||
//重置CD | ||
cd = 2f + Random.Range(-0.5f, 0.5f); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
public class PlayerControl : MonoBehaviour | ||
{ | ||
public BgControl bgControl; | ||
public int hp = 1; | ||
|
||
private Animator ani; | ||
private Rigidbody2D rBody; | ||
|
||
|
||
//是否在地面 | ||
private bool isGround; | ||
// Start is called before the first frame update | ||
void Start() | ||
{ | ||
ani = GetComponent<Animator>(); | ||
rBody = GetComponent<Rigidbody2D>(); | ||
|
||
} | ||
|
||
// Update is called once per frame | ||
void Update() | ||
{ | ||
//玩家死亡 | ||
if(hp <= 0) | ||
{ return; } | ||
//水平轴 | ||
float horizontal = Input.GetAxis("Horizontal"); | ||
if(horizontal != 0) | ||
{ | ||
//移动 | ||
ani.SetBool("isRun",true); | ||
//背景后移 | ||
bgControl.move(horizontal); | ||
} | ||
else | ||
{ | ||
ani.SetBool("isRun", false); | ||
} | ||
|
||
//跳跃 | ||
if(Input.GetKeyDown(KeyCode.Space)&&isGround == true) | ||
{ | ||
rBody.AddForce(Vector2.up * 170); | ||
AudioManager.Instance.PlayJump(); | ||
} | ||
} | ||
//进入碰撞 | ||
private void OnCollisionEnter2D(Collision2D collision) | ||
{ | ||
if(collision.collider.tag == "Ground") | ||
{ | ||
isGround = true; | ||
ani.SetBool("isJump", false); | ||
} | ||
} | ||
//离开碰撞 | ||
private void OnCollisionExit2D(Collision2D collision) | ||
{ | ||
if(collision.collider.tag == "Ground") | ||
{ | ||
isGround = false; | ||
ani.SetBool("isJump", true); | ||
} | ||
} | ||
|
||
//进入火圈触发区域 | ||
private void OnTriggerEnter2D(Collider2D collision) | ||
{ | ||
if(collision.tag == "Fire") | ||
{ | ||
hp--; | ||
Destroy(rBody); | ||
ani.SetBool("Die", true); | ||
AudioManager.Instance.PlayDie(); | ||
} | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.