C# 统一2D。不同类型的敌人==对他们造成不同类型的伤害?
我正在制作2D平台,我需要帮助。我没有使用任何统一框架来制作我的游戏,我想了解统一的简单选项 从本质上说。我制造了一个击昏我的玩家的敌人,它冲到我身上并处理dmg(还没有,我还没有玩家的健康系统)。但我也制作了一个骷髅敌人,它有动画,而且是用剑攻击的 我想让骷髅从我的玩家那里获得伤害。我为他做了和普雷维乌斯一样的事,但是我得到了一个例外。。。这让我想到了敌人的剧本,而不是骷髅。我必须说我对此感到非常困惑 下面我将粘贴一个异常和脚本C# 统一2D。不同类型的敌人==对他们造成不同类型的伤害?,c#,unity3d,C#,Unity3d,我正在制作2D平台,我需要帮助。我没有使用任何统一框架来制作我的游戏,我想了解统一的简单选项 从本质上说。我制造了一个击昏我的玩家的敌人,它冲到我身上并处理dmg(还没有,我还没有玩家的健康系统)。但我也制作了一个骷髅敌人,它有动画,而且是用剑攻击的 我想让骷髅从我的玩家那里获得伤害。我为他做了和普雷维乌斯一样的事,但是我得到了一个例外。。。这让我想到了敌人的剧本,而不是骷髅。我必须说我对此感到非常困惑 下面我将粘贴一个异常和脚本 NullReferenceException:对象引用未设置为对
NullReferenceException:对象引用未设置为对象的实例
DamageDeal.DealDmg()(位于Assets/Scripts/DamageDeal.cs:37)
CombatBehavior.fixeUpdate()(位于Assets/Scripts/CombatBehavior.cs:92)
敌方剧本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyScript : MonoBehaviour {
public float speed;
public float distance;
public int health;
public bool movingRight = true;
public Transform groundDetection;
public Transform wallDetection;
public bool otherEnemy;
public bool trap;
public LayerMask EnemyLayer;
public LayerMask TrapLayer;
public LayerMask WallLayer;
public Transform ColideDetector;
public float detectorRadius;
public BoxCollider2D CheckHeadBounce;
// Use this for initialization
void Start ()
{
//float distanceY = groundDetection.position.y + distance;
}
// Update is called once per frame
void Update ()
{
trap = Physics2D.OverlapCircle(ColideDetector.position, detectorRadius, TrapLayer);
otherEnemy = Physics2D.OverlapCircle(ColideDetector.position, detectorRadius, EnemyLayer);
if (health <= 0)
{
Destroy(gameObject);
}
transform.Translate(Vector2.right * speed * Time.deltaTime );
RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);
RaycastHit2D wallInfoR = Physics2D.Raycast(wallDetection.position, Vector2.right, distance, WallLayer);
RaycastHit2D wallInfoL = Physics2D.Raycast(wallDetection.position, Vector2.left, -distance, WallLayer);
if (groundInfo.collider == false || otherEnemy == true || wallInfoR == true || wallInfoL == true)
{
if(movingRight == true)
{
transform.eulerAngles = new Vector3(0, -180, 0);
movingRight = false;
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
movingRight = true;
}
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawLine(new Vector3(groundDetection.position.x, groundDetection.position.y), new Vector3(groundDetection.position.x, groundDetection.position.y + (-distance)));
if (movingRight == true)
{
Gizmos.DrawLine(new Vector3(wallDetection.position.x, wallDetection.position.y), new Vector3(wallDetection.position.x + distance, wallDetection.position.y));
}else if (movingRight == false)
{
Gizmos.DrawLine(new Vector3(wallDetection.position.x, wallDetection.position.y), new Vector3(wallDetection.position.x - distance, wallDetection.position.y));
}
}
public void HeadBounce()
{
}
public void TakeDmg(int damage)
{
health -= damage;
Debug.Log("damage TAKEN!");
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类敌人脚本:MonoBehavior{
公众浮标速度;
公众浮标距离;
公共卫生;
公共bool movingRight=真;
公共检测;
公共检测;
公敌;
公共陷阱;
公共层mask EnemyLayer;
公共层任务播放器;
公共层屏蔽墙层;
公共检测仪;
托拉迪乌斯公车;
公共车厢碰撞R2D CheckHeadBounce;
//用于初始化
无效开始()
{
//浮动距离y=地面检测。位置。y+距离;
}
//每帧调用一次更新
无效更新()
{
陷阱=物理2D.重叠圆(ColideDetector.position,detectorRadius,TrapLayer);
其他敌人=Physics2D.Overlap圆圈(共线探测器位置、探测器半径、EnemyLayer);
如果(health我认为从Physics2D.overlappCircleall返回的所有碰撞R2D都将附加SkeletonScript的假设是有缺陷的
Collider2D[] skeletonToDamage = Physics2D.OverlapCircleAll(attackPos1.position,attackRange, whatIsEnemy);
for (int i = 0; i < skeletonToDamage.Length; i++)
{
SkeletonScript skeletonScript = skeletonToDamage[i].GetComponent<SkeletonScript>();
collectedr2d[]skeletonodaign=Physics2D.overlappicrcleall(攻击位置1.位置、攻击范围、敌方);
对于(int i=0;i
最好先获取碰撞R2D组件所连接的父游戏对象,然后再确定该游戏对象是否连接了“SkeletonScript”组件。我是Unity和编程的初学者,如果我误解了您的意思,请原谅。我想到了这样的事情:对不起,我尝试了一些不同的组合,但是不起作用-请提供任何建议?学习是乐趣的一半。请尝试“代码var gameObject=skeletonodiage[i]。gameObject;var skeletonScript=gameObject.GetComponent();if(skeletonScript!=null){//PUT LOGIC HERE}“从那个gameObject检查它是否有SkeletonScript组件。祝你好运!谢谢你的建议@Andrew McCallum。我没有得到这个-很可能我对所有这些东西都太陌生了。很抱歉发布了太长的帖子和愚蠢的问题^^^。好吧,我已经找到了如何将损坏放在应该放在的位置上的方法……我已经使用了gameObject.SendMessageColisionEnter2D。我希望这将适合我的项目的其余部分。再次感谢:D。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkeletonScript : MonoBehaviour {
public float speed;
public float distance;
public int health;
private Rigidbody2D skeletonRB;
public bool movingRight = true;
public Transform groundDetection;
public Transform wallDetection;
public bool otherEnemy;
public bool trap;
public LayerMask EnemyLayer;
public LayerMask TrapLayer;
public LayerMask WallLayer;
public Transform ColideDetector;
public float detectorRadius;
public PlayerControls player;
public Animator skeletonAnim;
// Use this for initialization
void Start ()
{
skeletonRB = GetComponent<Rigidbody2D>();
player = FindObjectOfType<PlayerControls>();
}
// Update is called once per frame
void Update ()
{
MoveSkeleton();
SkeletonDeath();
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawLine(new Vector3(groundDetection.position.x, groundDetection.position.y), new Vector3(groundDetection.position.x, groundDetection.position.y + (-distance)));
if (movingRight == true)
{
Gizmos.DrawLine(new Vector3(wallDetection.position.x, wallDetection.position.y), new Vector3(wallDetection.position.x + distance, wallDetection.position.y));
}
else if (movingRight == false)
{
Gizmos.DrawLine(new Vector3(wallDetection.position.x, wallDetection.position.y), new Vector3(wallDetection.position.x - distance, wallDetection.position.y));
}
}
public void MoveSkeleton()
{
skeletonRB.transform.Translate(Vector2.right * speed * Time.deltaTime);
RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);
RaycastHit2D wallInfoR = Physics2D.Raycast(wallDetection.position, Vector2.right, distance, WallLayer);
RaycastHit2D wallInfoL = Physics2D.Raycast(wallDetection.position, Vector2.left, -distance, WallLayer);
if (groundInfo.collider == false || otherEnemy == true || wallInfoR == true || wallInfoL == true)
{
if (movingRight == true)
{
transform.eulerAngles = new Vector3(0, -180, 0);
movingRight = false;
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
movingRight = true;
}
}
}
public void TakeDmgSkeleton(int damageToSkeleton)
{
health -= damageToSkeleton;
Debug.Log("damage TAKEN!");
}
public void SkeletonDeath()
{
if (health <= 0)
{
skeletonAnim.Play("Skeleton_Dead", 0);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamageDealToSkeleton : MonoBehaviour {
public Transform attackPos1;
public float attackRange;
public LayerMask whatIsEnemy;
public int damageToSkeleton;
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPos1.position, attackRange);
}
public void ChooseOne()// this script was desperation move.
{
if (gameObject.tag == "Skeleton")
{
Debug.Log("Skeleton DMG");
DealDmgToSkeleton();
}
}
public void DealDmgToSkeleton()
{
if (attackPos1.gameObject.activeSelf == true)
{
Collider2D[] skeletonToDamage = Physics2D.OverlapCircleAll(attackPos1.position, attackRange, whatIsEnemy);
for (int i = 0; i < skeletonToDamage.Length; i++)
{
SkeletonScript skeletonScript = skeletonToDamage[i].GetComponent<SkeletonScript>();
skeletonScript.TakeDmgSkeleton(damageToSkeleton);
if (gameObject.GetComponent<PlayerControls>().facingRight == true)
{
skeletonScript.GetComponent<Rigidbody2D>().AddForce(new Vector2(15f, 15f), ForceMode2D.Impulse);
}
else if (gameObject.GetComponent<PlayerControls>().facingRight == false)
{
skeletonScript.GetComponent<Rigidbody2D>().AddForce(new Vector2(-15f, 15f), ForceMode2D.Impulse);
}
attackPos1.gameObject.SetActive(false);
}
}
}
}
Collider2D[] skeletonToDamage = Physics2D.OverlapCircleAll(attackPos1.position,attackRange, whatIsEnemy);
for (int i = 0; i < skeletonToDamage.Length; i++)
{
SkeletonScript skeletonScript = skeletonToDamage[i].GetComponent<SkeletonScript>();