Unity疯狂农场

实现目标

  • 用炮弹击中随机生成的动,动物消失,守护农场

用到的函数

  • 在Unity游戏引擎中,`InvokeRepeating`是一个用于在特定时间间隔重复执行某个函数的方法。对于你提供的这个InvokeRepeating调用:
InvokeRepeating("SpawnRandomAnimal", startDelay, spawnInterval);

每个参数的含义是:

1. `"SpawnRandomAnimal"`:这是一个字符串参数,代表了你要反复调用的函数名。这里是指要每隔一段时间就调用名为"SpawnRandomAnimal"的函数。

2. `startDelay`:这是一个浮点数类型的参数,表示首次调用"SpawnRandomAnimal"函数前需要等待的时间(以秒为单位)。例如,如果你设置为5,那么在调用此方法后的5秒,将会首次执行SpawnRandomAnimal函数。

3. `spawnInterval`:这也是一个浮点数类型的参数,它代表了连续调用"SpawnRandomAnimal"函数之间的间隔时间(同样以秒为单位)。比如,如果设置为2,那么每次执行完SpawnRandomAnimal函数后,都会等待2秒再次执行该函数,从而实现周期性的行为。

  • 区别
Destroy(gameObject);
Destroy(other.gameObject);

在Unity中,`Destroy(gameObject)`和`Destroy(other.gameObject)`都是用于销毁游戏对象(GameObjects)的函数调用,但它们作用的对象不同:

1. `Destroy(gameObject);`
   - 这个调用会销毁当前脚本所挂载的游戏对象。这里的`gameObject`是`this.gameObject`的简写形式,表示调用该代码的组件所属的游戏对象本身。一旦执行此命令,整个游戏对象及其所有子对象以及挂载在其上的所有组件都会被标记为待删除,并在下一帧或指定延迟时间后真正从内存中移除。

2. `Destroy(other.gameObject);`
   - 此调用则会销毁名为`other`引用的游戏对象。`other`通常是一个指向另一个游戏对象的引用,可能是通过碰撞检测、查找或者其他方式获取到的。与前一个例子一样,这个命令会销毁`other`所指向的游戏对象及其所有相关组件和子对象。

总结来说,两者的区别在于要销毁的游戏对象来源不同:一个是当前脚本所在的对象,另一个是由变量`other`指向的外部对象。

源码 

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

public class DetectCollisions : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnTriggerEnter(Collider other)
    {
        Destroy(this.gameObject);//this指代DetectCollisions这个类,gameObject指代与此类有关的游戏对象
        Destroy(other.gameObject);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnManager : MonoBehaviour
{
    public GameObject[] animalPrefabs;
    private float spawnPosZ = 20f;
    private float spawnRangeX=20f;
    private float startDelay = 2f;
    private float spawnInterval = 1.5f;
    //public int animalIndex;这样的话只能在规定生成下标,应做到随机
    void Start()
    {
        InvokeRepeating("SpawnRandomAnimal", startDelay, spawnInterval);//重复调用,startDelay秒后首次调用SpawnRandomAnimal,然后每间隔spawnInterval调用SpawnRandomAnimal
    }

    // Update is called once per frame
    void Update()
    {
        //if (Input.GetKey(KeyCode.S))按下S才会调用SpawnRandomAnimal()随机生成
        //{
        //    SpawnRandomAnimal();
        //}

    }
    void SpawnRandomAnimal()
    {

        Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ);
        int animalIndex = Random.Range(0, animalPrefabs.Length);//随机生成下标0-数组长度-1即0-2
        Instantiate(animalPrefabs[animalIndex], spawnPos, animalPrefabs[animalIndex].transform.rotation);

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

public class DestroyOutOfBounds : MonoBehaviour
{
    private float topBound=30f;
    private float lowerBound = -10;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (this.transform.position.z > topBound)
        {
            Destroy(gameObject);
        }else if (transform.position.z < lowerBound)
        {
            Debug.Log("Game Over");//当农民没有守护住农场,有动物从农民旁边路过,控制台打印Game Over
            Destroy(gameObject);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveForward : MonoBehaviour
{
    public float speed=40.0f;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float horizontallnput;
    public float speed = 10.0f;
    public float xRange = 10;
    public GameObject projectilePrefab;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        horizontallnput = Input.GetAxis("Horizontal");
        transform.Translate(Vector3.right * horizontallnput * Time.deltaTime*speed);
//这里应该是this.gameObject.transform.Translate,这里是把this.gameObject省略掉了
        if (transform.position.x < -xRange)
        {
            transform.position = new Vector3(-10,transform.position.y, transform.position.z);
        }
        if(transform.position.x> xRange)
        {
            transform.position = new Vector3(-10, transform.position.y, transform.position.z);
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Instantiate(projectilePrefab,transform.position,projectilePrefab.transform.rotation);
        }
    }
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值