英文:
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<Entity>();
}
// 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 == "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);
}
}
}
答案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<Entity>();
// new
player = GameObject.Find("PlayerNameInInspector");
}
// 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 == "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);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论