当前位置:   article > 正文

[Unity2D入门教程]简单制作仿植物大战僵尸游戏之③完善Defender植物和Attacker的相关细节(脚本,碰撞体)_unity制作植物大战僵尸小游戏

unity制作植物大战僵尸小游戏

 在创建完它们的Animation之后我们还要为他们特定的行径编写脚本,(比如Lizard鳄鱼需要向前移动),向日葵Trophy需要生产阳光,仙人掌Cactus需要向前发射子弹

我们先从向日葵Trophy开始写起,这里我们在Canvas底下创建一个Text

这层灰蒙蒙的是我创建的一个空对象Buttons下的子对象3D Object叫Quad并给它一个material

搞好这些之后我们给刚刚创建的Text一个脚本就叫Stars Display(别忘了挂载)

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using UnityEngine.UI;
  6. public class StarsDisplay : MonoBehaviour
  7. {
  8. [SerializeField] int stars = 100;
  9. Text starText;
  10. void Start()
  11. {
  12. starText = GetComponent<Text>();
  13. }
  14. //更新当前的星星(在变化以后)
  15. private void UpdateDisplay()
  16. {
  17. starText.text = stars.ToString();
  18. }
  19. //判断是否有足够的行星
  20. public bool HaveEnoughStars(int amount)
  21. {
  22. return stars >= amount ? true : false;
  23. }
  24. //增加星星
  25. public void AddStars(int amount)
  26. {
  27. stars += amount;
  28. UpdateDisplay();
  29. }
  30. //使用星星(创建植物)
  31. public void SpendStars(int amount)
  32. {
  33. if (stars >= amount)
  34. {
  35. stars -= amount;
  36. UpdateDisplay();
  37. }
  38. }
  39. }

编写脚本:

我们还要创建一个Defender的脚本给我们的向日葵Trophy

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Defender : MonoBehaviour
  5. {
  6. [SerializeField] int starCost = 100;
  7. public int GetStarCost()
  8. {
  9. return starCost;
  10. }
  11. public void AddStars(int amount)
  12. {
  13. FindObjectOfType<StarsDisplay>().AddStars(amount);
  14. }
  15. }

那我们到底要在哪里用这个AddStars(int)呢,这里就到了使用动画事件的时候了。

点击你想要的帧数,可以看到Samples 右边有个Add Events像个竖杠,这里就创建好了

点击后在inspector面板可以看到我们把刚刚创建的方法选择好改变它的int的值

当运行游戏时每当运行到这一帧的时候Star的值就会加一次3 

我们再来搞一下我们的仙人掌Cactus

他也需要一个Defender.cs来记录生成它需要花费多少Stars

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Defender : MonoBehaviour
  5. {
  6. [SerializeField] int starCost = 100;
  7. public int GetStarCost()
  8. {
  9. return starCost;
  10. }
  11. public void AddStars(int amount)
  12. {
  13. FindObjectOfType<StarsDisplay>().AddStars(amount);
  14. }
  15. }

还需要一个Shooter

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class Shooter : MonoBehaviour
  6. {
  7. [SerializeField] GameObject projectTile;
  8. [SerializeField] GameObject gun;
  9. AttackSpawner myLaneSpawner;
  10. private void Start()
  11. {
  12. SetLaneSpawner();
  13. }
  14. private void Update()
  15. {
  16. if(IsAttackerInLand())
  17. {
  18. Debug.Log("Shoot");
  19. }
  20. else
  21. {
  22. Debug.Log("Sit and Lane");
  23. }
  24. }
  25. private void SetLaneSpawner()
  26. {
  27. AttackSpawner[] spawners = FindObjectsOfType<AttackSpawner>();
  28. //找到这行路的所有敌人
  29. foreach(var spawner in spawners)
  30. {
  31. //判断是不是在我们这一行
  32. bool IsCloseEnough = (Mathf.Abs( spawner.transform.position.y - transform.position.y ) <= Mathf.Epsilon);
  33. if (IsCloseEnough)
  34. {
  35. myLaneSpawner = spawner; //找到我这条路的生成敌人
  36. }
  37. }
  38. }
  39. private bool IsAttackerInLand()
  40. {
  41. //如果我这条路上的敌人数量小于等于0return false
  42. if(myLaneSpawner.transform.childCount <= 0)
  43. {
  44. return false;
  45. }
  46. else
  47. {
  48. return true;
  49. }
  50. }
  51. public void Fire()
  52. {
  53. Instantiate(projectTile, gun.transform.position, transform.rotation); //生成子弹
  54. }
  55. }

这里我们创建好的空对象Gun就作为发射点

同样我们还要创建一个子弹的Prefab,这里大伙应该都看得懂就不细讲了直接上图

我们还需要给它一个Projectile名字的脚本

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Projectile : MonoBehaviour
  5. {
  6. [SerializeField] float speed = 1f;
  7. [SerializeField] float damage = 50f;
  8. void Update()
  9. {
  10. transform.Translate(Vector2.right * speed * Time.deltaTime);
  11. }
  12. private void OnTriggerEnter2D(Collider2D other)
  13. {
  14. var health = other.GetComponent<Health>();
  15. var attacker = other.GetComponent<Attacker>();
  16. if(attacker && health)
  17. {
  18. health.DealDamage(damage);
  19. Destroy(gameObject);
  20. }
  21. }
  22. }

 创建敌人:

  我们先给它设置好这两项就到脚本的部分

 敌人当然需要生命值:

        Health的脚本如下

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class Health : MonoBehaviour
  6. {
  7. [SerializeField] float health = 100f;
  8. [SerializeField] GameObject deathVFX;
  9. public void DealDamage(float damage)
  10. {
  11. health -= damage;
  12. if(health <= 0)
  13. {
  14. TriggerDeathVFX();
  15. Destroy(gameObject);
  16. }
  17. }
  18. //生成爆炸特效
  19. private void TriggerDeathVFX()
  20. {
  21. if (!deathVFX) return;
  22. GameObject deathVFXObject = Instantiate(deathVFX, transform.position, transform.rotation);
  23. Destroy(deathVFXObject, 1f); //1秒后销毁生成的爆炸特效
  24. }
  25. }

 这是向左移动的脚本

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Attacker : MonoBehaviour
  5. {
  6. [Range(0, 5)]
  7. float currentSpeed = 1f;
  8. void Update()
  9. {
  10. transform.Translate(Vector2.left * currentSpeed * Time.deltaTime);
  11. }
  12. public void SetMovementSpeed(float speed)
  13. {
  14. currentSpeed = speed;
  15. }
  16. }

进入游戏,敌人就会开始移动了,

有关敌人生成的位置以及点击植物来在场景中生成植物的内容,放在下一篇来讲。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/770505
推荐阅读
相关标签
  

闽ICP备14008679号