这里主要说下脚本实现,我创建了几个脚本,分别是玩家脚本(执行玩家本身动画),背景脚本(背景以及地面滚动),死亡脚本(死亡判定),金币脚本(计数判断),声音脚本(单例获取声音资源)
1.动画状态机 使用了跑步和跳起 还有 死亡 三种动画 使用2个参数控制
- 玩家脚本 玩家和地面都要加上碰撞体 (玩家需要增加刚体锁定旋转,否则会出现人物倒地情况),用于判断和地面的碰撞情况,来控制跳起次数。建议常用的物体设置为预制体 方便后期复用 和调整
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerControl : MonoBehaviour
{
//刚体组件
private Rigidbody2D rbody;
private Animator ani;
//是否碰撞了地面
private bool isGround;
// Start is called before the first frame update
void Start()
{
rbody = GetComponent<Rigidbody2D>();
ani = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//监听键盘空格按下
if (Input.GetKeyDown(KeyCode.Space)) {
Jump();
}
}
public void Jump() {
if (isGround == true) {
//刚体添加向上的力
rbody.AddForce(Vector2.up * 400);
audiomanager.instance.play("跳");
}
}
//产生碰撞
private void OnCollisionEnter2D(Collision2D collision)
{
//判断地面标签
if (collision.collider.tag == "ground")
{
isGround = true;
ani.SetBool("isJump", false);
}
if (collision.collider.tag == "die")
{
audiomanager.instance.play("Boss死了");
ani.SetBool("isdie", true);
}
}
//结束碰撞
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.collider.tag == "ground")
{
isGround = false;
ani.SetBool("isJump", true);
}
}
}
2.金币脚本 金币增加碰撞体中的触发器 判断碰撞和自己销毁 并统计金币计数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class coinControl : MonoBehaviour
{
//碰撞器与触发器 产生交互后自动调用
private void OnTriggerEnter2D(Collider2D collision)
{
//播放吃金币的声音
audiomanager.instance.play("金币");
//销毁自己
Destroy(gameObject);
}
}
3.声音脚本 单例模式获取声音资源 对外提供播放方法 根据声音的名称获取声音片段
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class audiomanager : MonoBehaviour
{
//单例
public static audiomanager instance;
//播放组件
private AudioSource player;
// Start is called before the first frame update
void Start()
{
instance = this;
player = GetComponent<AudioSource>();
}
public void play(string name) {
//获取音频片段
AudioClip clip = Resources.Load<AudioClip>(name);
//播放片段
player.PlayOneShot(clip);
}
// Update is called once per frame
void Update()
{
}
}
4.地面脚本 用于地面移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class groundControl : MonoBehaviour
{
public float speed = 2f;
//随机地面数组
public GameObject[] GroundPrefabs;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
foreach (Transform tran in transform)
{
//获取子物体的位置
Vector3 pos = tran.position;
//向左移动
pos.x -= speed * Time.deltaTime;
if (pos.x < -7.2f)
{
//实例化随机新地面
Transform newTrans = Instantiate(GroundPrefabs[Random.Range(0, GroundPrefabs.Length)],transform).transform;
Vector2 newpos = newTrans.position;
newpos.x = pos.x + 7.2f * 2;
newTrans.position = newpos;
//pos.x += 7.2f * 2;
//销毁出屏幕的地面
Destroy(tran.gameObject);
}
tran.position = pos;
}
}
}
5.背景脚本 速度慢与地面
using System.Collections; using System.Collections.Generic; using UnityEngine; public class bgcontrol : MonoBehaviour { public float speed = 0.2f; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { foreach (Transform tran in transform) { //获取子物体的位置 Vector3 pos = tran.position; //向左移动 pos.x -= speed * Time.deltaTime; if (pos.x<-7.2f) { pos.x += 7.2f * 2; } tran.position = pos; } } }
总结 主要学习了碰撞体的简单应用 以及状态机的简单使用