Unity C#: 添加代码以转换敌人位置

huangapple go评论65阅读模式
英文:

Unity C#: Adding Code to Transform Enemy Position

问题

我一直在Unity C#中开发以下脚本,该脚本分配给了一个敌人。我想要添加使敌人能够上下移动或左右移动的代码,但我在尝试时遇到了问题。非常感谢有关添加此功能的建议。

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

public class Enemy : MonoBehaviour
{
    public float damageRadius = 0.5f;
    public int meleeDamage = 200;

    [HideInInspector]
    Entity entityScript;

    void Start()
    {
        entityScript = GetComponent<Entity>();
    }

    void Update()
    {
        CheckForPlayerAndDamage();
        CheckForHealthAndDie();
    }

    void CheckForPlayerAndDamage()
    {
        var colliders = Physics2D.OverlapCircleAll(transform.position, damageRadius);
        foreach (var collider in colliders)
        {
            if (collider.gameObject.tag == "Player")
            {
                collider.gameObject.GetComponent<Entity>().ApplyDamage(meleeDamage);
            }
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, damageRadius);
    }

    public void CheckForHealthAndDie()
    {
        if (entityScript.isDead)
        {
            Destroy(gameObject);
        }
    }
}
英文:

I've been working on the following script in Unity C#, which is assigned to an enemy. I would like to add code that enables the enemy to move up and down, or side to side, but am having trouble getting anything to work. Advice on adding this functionality is very appreciated.

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

public class Enemy : MonoBehaviour
{
    public float damageRadius = 0.5f;
    public int meleeDamage = 200;

    [HideInInspector]
    Entity entityScript;
    // Start is called before the first frame update
    void Start()
    {
        entityScript = GetComponent&lt;Entity&gt;();

    }

    // Update is called once per frame
    void Update()
    {
        CheckForPlayerAndDamage();
        CheckForHealthAndDie();
    }


    void CheckForPlayerAndDamage()
    {
        var colliders = Physics2D.OverlapCircleAll(transform.position, damageRadius);
        foreach (var collider in colliders)
        {
            if (collider.gameObject.tag == &quot;Player&quot;)
            {
                collider.gameObject.GetComponent&lt;Entity&gt;().ApplyDamage(meleeDamage);
            }
        }
    }
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, damageRadius);
    }

    public void CheckForHealthAndDie()
    {
        if(entityScript.isDead)
        {
            Destroy(gameObject);
        }
    }
}


答案1

得分: 1

以下是翻译好的代码部分:

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

public class Enemy : MonoBehaviour
{
    public float damageRadius = 0.5f;
    public int meleeDamage = 200;

    private Transform player;
    private float speed = 10f;

    [HideInInspector]
    Entity entityScript;

    void Start()
    {
        entityScript = GetComponent<Entity>();
        player = GameObject.Find("PlayerNameInInspector").transform;
    }

    void Update()
    {
        CheckForPlayerAndDamage();
        CheckForHealthAndDie();
        transform.position = Vector3.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
        transform.LookAt(player.position, Vector3.up);
    }

    void CheckForPlayerAndDamage()
    {
        var colliders = Physics2D.OverlapCircleAll(transform.position, damageRadius);
        foreach (var collider in colliders)
        {
            if (collider.gameObject.tag == "Player")
            {
                collider.gameObject.GetComponent<Entity>().ApplyDamage(meleeDamage);
            }
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, damageRadius);
    }

    public void CheckForHealthAndDie()
    {
        if (entityScript.isDead)
        {
            Destroy(gameObject);
        }
    }
}

希望这有所帮助。如果有任何其他问题,请随时提出。

英文:

You can try this:

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

public class Enemy : MonoBehaviour
{
    public float damageRadius = 0.5f;
    public int meleeDamage = 200;
    
    // new
    private Transform player;
    private float speed = 10f;

    [HideInInspector]
    Entity entityScript;
    // Start is called before the first frame update
    void Start()
    {
        entityScript = GetComponent&lt;Entity&gt;();
        
        // new
        player = GameObject.Find(&quot;PlayerNameInInspector&quot;);
    }

    // Update is called once per frame
    void Update()
    {
        CheckForPlayerAndDamage();
        CheckForHealthAndDie();
        
        //new 
        transform.position = Vector3.MoveTowards( transform.position, player.transform.position, speed * Time.deltaTime );
        transform.LookAt( player.transform.position, Vector3.up );
    }


    void CheckForPlayerAndDamage()
    {
        var colliders = Physics2D.OverlapCircleAll(transform.position, damageRadius);
        foreach (var collider in colliders)
        {
            if (collider.gameObject.tag == &quot;Player&quot;)
            {
                collider.gameObject.GetComponent&lt;Entity&gt;().ApplyDamage(meleeDamage);
            }
        }
    }
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, damageRadius);
    }

    public void CheckForHealthAndDie()
    {
        if(entityScript.isDead)
        {
            Destroy(gameObject);
        }
    }
}

huangapple
  • 本文由 发表于 2023年6月15日 01:37:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定