您当前的位置: 首页 >  dangoxiba 游戏

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

dangoxiba 发布时间:2022-04-12 14:21:53 ,浏览量:4

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

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

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

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class StarsDisplay : MonoBehaviour
{
    [SerializeField] int stars = 100;
    Text starText;
    void Start()
    {
        starText = GetComponent();
    }
    //更新当前的星星(在变化以后)
    private void UpdateDisplay()
    {
        starText.text = stars.ToString();
    }
    //判断是否有足够的行星
    public bool HaveEnoughStars(int amount)
    {
        return stars >= amount ? true : false;
    }
    //增加星星
    public void AddStars(int amount)
    {
        stars += amount;
        UpdateDisplay();
    }
    //使用星星(创建植物)
    public void SpendStars(int amount)
    {
        if (stars >= amount)
        {
            stars -= amount;
            UpdateDisplay();
        }
    }
    
}
编写脚本:

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Defender : MonoBehaviour
{
    [SerializeField] int starCost = 100;

    public int GetStarCost()
    {
        return starCost;
    }
    public void AddStars(int amount)
    {
        FindObjectOfType().AddStars(amount);
    }
}

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

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

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

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

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

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Defender : MonoBehaviour
{
    [SerializeField] int starCost = 100;

    public int GetStarCost()
    {
        return starCost;
    }
    public void AddStars(int amount)
    {
        FindObjectOfType().AddStars(amount);
    }
}

还需要一个Shooter

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooter : MonoBehaviour
{
    [SerializeField] GameObject projectTile;
    [SerializeField] GameObject gun;
    AttackSpawner myLaneSpawner;
    private void Start()
    {
        SetLaneSpawner();
    }
    private void Update()
    {
        if(IsAttackerInLand())
        {
            Debug.Log("Shoot");
        }
        else
        {
            Debug.Log("Sit and Lane");
        }
    }
    private void SetLaneSpawner()
    {
        AttackSpawner[] spawners = FindObjectsOfType();
        //找到这行路的所有敌人
        foreach(var spawner in spawners)
        {
            //判断是不是在我们这一行
            bool IsCloseEnough = (Mathf.Abs( spawner.transform.position.y - transform.position.y )             
关注
打赏
1688896170
查看更多评论
0.0494s