以下是一个简单的游戏脚本示例,可以实现自动找路和自动杀怪:

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

public class GameScript : MonoBehaviour {

    public Transform player;
    public Transform target;
    public float speed = 5f;
    public float attackRange = 2f;
    public float attackCooldown = 2f;
    private float lastAttackTime = 0f;

    // Use this for initialization
    void Start () {
        // 初始化
    }
    
    // Update is called once per frame
    void Update () {
        // 自动找路
        if (Vector3.Distance(player.position, target.position) > attackRange) {
            Vector3 direction = (target.position - player.position).normalized;
            player.Translate(direction * speed * Time.deltaTime, Space.World);
        }
        
        // 自动攻击
        if (Time.time - lastAttackTime > attackCooldown && Vector3.Distance(player.position, target.position) <= attackRange) {
            lastAttackTime = Time.time;
            // 发起攻击
        }
    }
}

这个脚本假设有一个player和一个targetplayer会自动找路到target的位置,当playertarget距离小于等于攻击范围时,会自动攻击。speed表示移动速度,attackRange表示攻击范围,attackCooldown表示攻击冷却时间。你需要根据你的实际游戏情况修改这些值。在Update函数中处理自动寻路和自动攻击的逻辑,你需要根据你的实际游戏情况实现具体的攻击逻辑。

用c#怎写一个游戏脚本如自动找路自动杀怪

原文地址: https://www.cveoy.top/t/topic/bdYZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录