《下地狱吧》Unity制作
本文章只提供代码,资源请自行到资源商店挑选自己喜欢的模型,音乐,图片
人物代码
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
///<summary>
///
///</summary>
public class player : MonoBehaviour
{
public float Speed=10f;
public int hp;
public GameObject GameObject;
public GameObject hpbar;
public Text Text;
public int sorce;
public float time;
public AudioSource audioSource;
public GameObject button;
public void Start()
{
hp = 10;//角色血量
sorce = 0;//层数
time = 0;//所用时间
audioSource = GetComponent<AudioSource>();//获取音频组件
}
private void Update()
{
//设置人物移动
if(Input.GetKey(KeyCode.A))
{
this.transform.Translate(-Speed* Time.deltaTime,0,0);
this.transform.GetComponent<SpriteRenderer>().flipX = true;//让他可以左右转向
transform.GetComponent<Animator>().SetBool("Grounded",true);//激活动画,这个根据自己的资源自己设置参数,我这个是BooL型
}else if(Input.GetKey(KeyCode.S))
{
this.transform.Translate(0,-Speed*Time.deltaTime,0);
}
else if (Input.GetKey(KeyCode.D))
{
this.transform.Translate(Speed* Time.deltaTime,0,0);
this.transform.GetComponent<SpriteRenderer>().flipX = false;
transform.GetComponent<Animator>().SetBool("Grounded", true);
}
else if (Input.GetKey(KeyCode.W))
{
this.transform.Translate(0, Speed * Time.deltaTime, 0);
transform.GetComponent<Animator>().SetBool("sb", true);
}
else
{
transform.GetComponent<Animator>().SetBool("Grounded",false);//恢复初始状态
transform.GetComponent<Animator>().SetBool("sb", false);
}
textcnm();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag=="up")
{
GameObject.GetComponent<BoxCollider2D>().enabled=false;//如果人物碰到顶部,人物的碰撞感应消失,就会降落
hpchange(-3);
transform.GetComponent<Animator>().SetTrigger("Hurt");
transform.GetComponent<Animator>().SetBool("fall", false);
collision.transform.GetComponent<AudioSource>().Play();//播放天花板音频
}
else if(collision.gameObject.tag=="floor1")
{
if (collision.contacts[0].normal == new Vector2(0f, 1f))//只有接触法向量指向上(人物接触物体上表面才执行),才能触发
{
GameObject = collision.gameObject;
hpchange(1);
transform.GetComponent<Animator>().SetBool("fall", false);
collision.transform.GetComponent<AudioSource>().Play();
}
}else if(collision.gameObject.tag=="floor2")
{
if (collision.contacts[0].normal == new Vector2(0f, 1f))
{ GameObject = collision.gameObject;
hpchange(-3);
transform.GetComponent<Animator>().SetTrigger("Hurt");
transform.GetComponent<Animator>().SetBool("fall", false);
collision.transform.GetComponent<AudioSource>().Play();
}
}else
{
transform.GetComponent<Animator>().SetBool("fall",true);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag=="deadline")//掉出地图外才会执行,死亡线自己设定
{
hp = 0;
Time.timeScale = 0;
button.gameObject.SetActive(true);
}
}
private void hpchange(int num)
{
hp += num;
if (hp>10)
{
hp = 10;
}else if(hp<0){
hp = 0;
transform.GetComponent<Animator>().SetBool("hurt",true);
audioSource.Play();
Destroy(gameObject,1f);
Time.timeScale = 0;
button.gameObject.SetActive(true);//激活按钮
}
hpcnm();
}
private void hpcnm()//控制血条显示
{
for(int i = 0;i<hpbar.transform.childCount;i++)
{
if(hp>i)
{
hpbar.transform.GetChild(i).gameObject.SetActive(true);//满足条件的血条才能显示
}
else
{
hpbar.transform.GetChild(i).gameObject.SetActive(false);
}
}
}
private void textcnm()
{
time += Time.deltaTime;
if(time>2f)
{
sorce++;
time = 0;
}
Text.text = "地狱" + sorce.ToString() + "层";//地狱层数显示,每两秒显示一次
}
public void jk()
{
Time.timeScale = 1f;
SceneManager.LoadScene("SampleScene");//重新加载场景
}
}
木板移动
public class move : MonoBehaviour
{
public float speed;
public void Start()
{
speed = 2.0f;
}
private void Update()
{
this.transform.Translate(0, speed * Time.deltaTime, 0);//木板向上移动
if (this.transform.position.y > 6.0f)//若超过指定界限,就摧毁这个物体
{
Destroy(gameObject);
this.transform.parent.GetComponent<floor>().create();
}
}
}
create函数的实现
using UnityEngine;
///<summary>
///
///</summary>
public class floor : MonoBehaviour
{
public GameObject []GameObject;
public void create()
{
int op=Random.Range(0, 3);
GameObject jk= Instantiate(GameObject[op],this.transform);//生成木块
jk.transform.position = new Vector2(Random.Range(-5.83f, 8f), -3f);
}
}
`` 本人小白,若有错误或者可以完善的地方都能私聊我哟!