【制作100个unity游戏之29】使用unity复刻经典游戏《愤怒的小鸟》(完结,附带项目源码)(上):https://developer.aliyun.com/article/1553534
相机跟随
新增FollowTarget,控制相机跟随
public class FollowTarget : MonoBehaviour { // 要跟随的对象 private Transform target; // 跟随的平滑速度 public float smoothSpeed = 2f; void Update() { // 确保目标不为空 if (target != null) { // 获取当前物体的位置 Vector3 position = transform.position; // 将目标的 x 轴位置赋值给当前物体的位置 position.x = target.position.x; position.x = Mathf.Clamp(position.x, 0, 20);//限制 // 使用插值函数 Lerp 平滑移动当前物体到新位置 transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * smoothSpeed); } } // 设置目标的方法,可以从外部调用此方法来设置跟随的目标 public void SetTarget(Transform newTarget) { // 将传入的 Transform 赋值给目标 this.target = newTarget; } }
修改GameManager调用
public void LoadNextBird() { index++; if (index >= birdList.Length) { GameEnd(); } else { birdList[index].SetStart(Slingshot.Instance.shootPoint.transform.position); Camera.main.GetComponent<FollowTarget>().SetTarget(birdList[index].transform);//设置摄像机跟随目标 } }
配置
效果
加分特效
配置加分动画效果
新增ScoreManager
public class ScoreManager : MonoBehaviour { public static ScoreManager Instance { get; private set; } // 预设体 public GameObject scorePrefab; // 不同分数对应的精灵数组 public Sprite[] score3000; public Sprite[] score5000; public Sprite[] score10000; // 字典,用于根据分数查找对应的精灵数组 private Dictionary<int, Sprite[]> scoreDict; private void Awake() { Instance = this; } private void Start() { scoreDict = new Dictionary<int, Sprite[]> { { 3000, score3000 }, { 5000, score5000 }, { 10000, score10000 } }; } // 显示分数的方法 public void ShowScore(Vector3 position, int score) { // 实例化分数预设体 GameObject scoreGo = Instantiate(scorePrefab, position, Quaternion.identity); // 根据分数获取对应的精灵数组 Sprite[] scoreArray; if (scoreDict.TryGetValue(score, out scoreArray)) { // 随机选择一个精灵 int index = Random.Range(0, scoreArray.Length); Sprite sprite = scoreArray[index]; // 设置SpriteRenderer的sprite属性 scoreGo.GetComponent<SpriteRenderer>().sprite = sprite; } // 在1秒后销毁显示的分数对象 Destroy(scoreGo, 1f); } }
修改Pig调用
public class Pig : Destructiable { public int score = 3000; public override void Dead() { base.Dead(); GameManager.Instance.OnPigDead(); ScoreManager.Instance.ShowScore(transform.position, score); } }
效果
不同定义技能的鸟
修改Bird,定义可重写的不同时段技能方法
bool isFlying;//是否飞行 bool isUserdSkill;//是否已使用技能 //抬起触发事件 private void OnMouseUp() { if (state == BirdState.BeforeShoot) { isMouseDown = false; Slingshot.Instance.EndDraw(); Fly(); isFlying = true; } } void Update() { switch (state) { case BirdState.Waiting: break; case BirdState.BeforeShoot: MoveControl(); break; case BirdState.AfterShoot: StopControl(); SkillControl(); break; case BirdState.WaitToDie: break; default: break; } } //使用技能 void SkillControl(){ if(isUserdSkill) return; if(Input.GetMouseButtonDown(0)){ isUserdSkill = true; if(isFlying == true){ FlytingSkill(); } FullTimeSkill(); } } //飞行技能 protected virtual void FlytingSkill(){ } //全时段技能 protected virtual void FullTimeSkill(){ } private void OnCollisionEnter2D(Collision2D other) { if(state == BirdState.AfterShoot){ isFlying = false; } }
加速鸟
//加速鸟 public class SpeedUpBird : Bird { protected override void FlytingSkill() { rb.velocity = rb.velocity * 2; } }
回旋鸟
//回旋鸟 public class SlalomBird : Bird { protected override void FlytingSkill() { Vector2 velocity = rb.velocity; velocity.x = -velocity.x; rb.velocity = velocity; Vector3 scale = transform.localScale; scale.x = -scale.x; transform.localScale = scale; } }
爆炸鸟
//爆炸鸟 public class BoomBird : Bird { public float boomRadius = 2.5f;//爆炸半径 protected override void FullTimeSkill() { Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, boomRadius); foreach (Collider2D collider in colliders) { Destructiable des = collider.GetComponent<Destructiable>(); if(des != null) des.TakeDamage(Int32.MaxValue); } state = BirdState.WaitToDie; LoadNextBird(); } }
效果
轨迹预测
分析
现在我们翻译成代码,手指抬起的时候,计算速度向量:
// 放大速度倍数 float factor = 4f; m_distance = Vector2.Distance(m_startPoint, m_endPoint); m_direction = (m_startPoint - m_endPoint).normalized; Vector2 speed = m_direction * m_distance * factor;
有了这个speed,我们就可以预测轨迹了。
假设鸟的坐标为Vector3 birdPos,根据斜抛路径公式,那么预测曲线轨迹点的坐标(posX, posY)就是这样:
float posX = birdPos.x + speed.x * t; float posY = birdPos.x + speed.y * t - 0.5f * Physics2D.gravity.magnitude * t * t;
另外,我们需要让鸟根据初始的speed做斜抛运动,这里要用到Rigidbody2D的AddForce接口,例:
rigidbody2D.AddForce(speed, ForceMode2D.Impulse);
实操
为了描绘曲线,我们用这个小云团作为一个个点,将其做成预设
新增Trajectory曲线预测器代码
public class Trajectory : MonoBehaviour { /// <summary> /// 预测点的数量 /// </summary> [SerializeField] private int m_dotsNum = 20; /// <summary> /// 点物体的父节点 /// </summary> [SerializeField] private GameObject m_dotsParent; /// <summary> /// 点预设 /// </summary> [SerializeField] private GameObject m_dotsPrefab; /// <summary> /// 点间距 /// </summary> [SerializeField] private float m_dotSpacing = 0.01f; /// <summary> /// 点的最小缩放 /// </summary> [SerializeField] [Range(0.01f, 0.3f)] private float m_dotMinScale = 0.1f; /// <summary> /// 点的最大缩放 /// </summary> [SerializeField] [Range(0.3f, 1f)] private float m_dotMaxScale = 1f; private Transform[] m_dotsList; private Vector2 m_pos; private float m_timeStamp; private void Start() { Hide(); PrepareDots(); } /// <summary> /// 准备轨迹点 /// </summary> private void PrepareDots() { m_dotsList = new Transform[m_dotsNum]; m_dotsPrefab.transform.localScale = Vector3.one * m_dotMaxScale; float scale = m_dotMaxScale; float scaleFactor = scale / m_dotsNum; for (int i = 0; i < m_dotsNum; ++i) { var dot = Instantiate(m_dotsPrefab).transform; dot.parent = m_dotsParent.transform; dot.localScale = Vector3.one * scale; if (scale > m_dotMinScale) scale -= scaleFactor; m_dotsList[i] = dot; } } /// <summary> /// 更新点坐标 /// </summary> /// <param name="birdPos">鸟的坐标</param> /// <param name="pushSpeed">初始速度向量</param> public void UpdateDots(Vector2 birdPos, Vector2 pushSpeed) { m_timeStamp = m_dotSpacing; for (int i = 0; i < m_dotsNum; ++i) { m_pos.x = birdPos.x + pushSpeed.x * m_timeStamp; m_pos.y = birdPos.y + pushSpeed.y * m_timeStamp - 0.5f * Physics2D.gravity.magnitude * m_timeStamp * m_timeStamp; m_dotsList[i].position = m_pos; m_timeStamp += m_dotSpacing; } } /// <summary> /// 显示预测轨迹 /// </summary> public void Show() { m_dotsParent.SetActive(true); } /// <summary> /// 隐藏预测轨迹 /// </summary> public void Hide() { m_dotsParent.SetActive(false); } }
配置
修改Bird
private Trajectory trajectory;// 轨迹预测器 trajectory = FindObjectOfType<Trajectory>(); //按下触发事件 private void OnMouseDown() { if (state == BirdState.BeforeShoot) { isMouseDown = true; Slingshot.Instance.StartDraw(transform); // 显示轨迹 trajectory.Show(); } } //抬起触发事件 private void OnMouseUp() { if (state == BirdState.BeforeShoot) { isMouseDown = false; Slingshot.Instance.EndDraw(); Fly(); isFlying = true; // 隐藏轨迹 trajectory.Hide(); GetComponent<TestMyTrail>().heroAttack(); } } private void MoveControl() { if (isMouseDown) { transform.position = GetMousePosition();//跟随鼠标 Vector3 mouseDir = Slingshot.Instance.shootPoint.position - transform.position; m_pushSpeed = mouseDir.normalized * mouseDir.magnitude * force;//力向量 trajectory.UpdateDots(transform.position, m_pushSpeed);更新预测点坐标 } }
效果
拖尾效果
参考:等等写
效果
UI界面
下面给出一些界面的参考。你也可以按自己喜欢的样子制作
暂停
游戏结束界面
加载界面
菜单界面
关卡选择界面
添加UI特效
添加动画,UI闪光效果
可以查看文章:【推荐100个unity插件之11】Shader实现UGUI的特效——UIEffect为 Unity UI提供视觉效果组件
效果
UI粒子效果
【推荐100个unity插件之12】UGUI的粒子效果(UI粒子)—— Particle Effect For UGUI (UIParticle)
添加UI粒子
记得修改Group Id,每个的id不能一致
注意父物体的z轴不能为0
配置
纹理选择星星图片
效果
音效
可以参考这篇文章制作即可:【unity小技巧】Unity音乐和音效管理器
完结
其中还有一些细节这里就不多说了,自己按照喜欢去配置即可