Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 攻击与攻击;使用Unity 3d对2d游戏造成伤害_C#_Unity3d - Fatal编程技术网

C# 攻击与攻击;使用Unity 3d对2d游戏造成伤害

C# 攻击与攻击;使用Unity 3d对2d游戏造成伤害,c#,unity3d,C#,Unity3d,有点小问题。我制作了一些无错误的脚本,但在Unity 3d的精灵工厂中尝试启动攻击动画时遇到了困难,无法在角色或敌人攻击时启动动画。他们俩都懒散得要死。我在我的主角和敌人身上都附加了必要的刚体和盒子对撞机,但没有任何互动。我已经设法使用Sprite Factory为我的角色激活了跑步动画(下面的代码作为示例) 使用UnityEngine; 使用系统集合; 使用FactorySprite=SpriteFactory.Sprite; 公共课步行:单一行为{ //您忘记设置表示精灵的变量的名称 私人工

有点小问题。我制作了一些无错误的脚本,但在Unity 3d的精灵工厂中尝试启动攻击动画时遇到了困难,无法在角色或敌人攻击时启动动画。他们俩都懒散得要死。我在我的主角和敌人身上都附加了必要的刚体和盒子对撞机,但没有任何互动。我已经设法使用Sprite Factory为我的角色激活了跑步动画(下面的代码作为示例)

使用UnityEngine;
使用系统集合;
使用FactorySprite=SpriteFactory.Sprite;
公共课步行:单一行为{
//您忘记设置表示精灵的变量的名称
私人工厂雪碧雪碧;
//用于初始化
无效开始(){
sprite=GetComponent();//已编辑
}
//每帧调用一次更新
无效更新(){
if(Input.GetKey(KeyCode.RightArrow)){
sprite.Play(“Walk”)//嘿,记住C区分大小写:)
向量3位置=变换位置;
pos.x+=Time.deltaTime*12;
transform.position=pos;
}else if(Input.GetKey(KeyCode.LeftArrow)){
sprite.Play(“Walk0”);//嘿,记住C#区分大小写:)
向量3位置=变换位置;
位置x+=时间增量*-12;
transform.position=pos;
}
否则{
雪碧。停止();
}
}
}
脚本位于[玩家攻击]下方

using UnityEngine;
using System.Collections;public class PlayerAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;

// Use this for initialization
void Start () {
    attackTimer = 0;
    coolDown = 2.0f;
}

// Update is called once per frame
void Update () {
    if(attackTimer > 0)
        attackTimer -= Time.deltaTime;

    if(attackTimer < 0)
        attackTimer = 0;

    if(Input.GetKeyUp(KeyCode.F)) {
        if(attackTimer == 0) {
            Attack();
            attackTimer = coolDown;
        }
    }

}

private void Attack() {
    float distance = Vector3.Distance(target.transform.position, transform.position);

    Vector3 dir = (target.transform.position - transform.position).normalized;

    float direction = Vector3.Dot(dir, transform.forward);

    if(distance < 2.5f) {
        if(direction > 0) {
            EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
            eh.AddjustCurrentHealth(-10);
        }
    }
}
}
使用UnityEngine;
使用系统集合;公共类剧目:单一行为{
公共游戏对象目标;
公共浮动攻击计时器;
公共浮子冷却;
//用于初始化
无效开始(){
攻击计时器=0;
冷却时间=2.0f;
}
//每帧调用一次更新
无效更新(){
如果(攻击计时器>0)
attackTimer-=Time.deltaTime;
如果(攻击计时器<0)
攻击计时器=0;
if(Input.GetKeyUp(KeyCode.F)){
如果(攻击计时器==0){
攻击();
攻击计时器=冷却时间;
}
}
}
私有无效攻击(){
浮动距离=矢量3.距离(target.transform.position,transform.position);
Vector3 dir=(target.transform.position-transform.position).normalized;
浮点方向=矢量3.Dot(dir,transform.forward);
如果(距离<2.5f){
如果(方向>0){
EnemyHealth eh=(EnemyHealth)target.GetComponent(“EnemyHealth”);
eh.AddjustCurrentHealth(-10);
}
}
}
}
玩家健康:

using UnityEngine;
using System.Collections;
public class PlayersHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;

public float healthBarLength;

// Use this for initialization
void Start () {
    healthBarLength = Screen.width / 2;
}

// Update is called once per frame
void Update () {
    AddjustCurrentHealth(0);
}

void OnGUI() {
    GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}

public void AddjustCurrentHealth(int adj) {
    curHealth += adj;

    if(curHealth < 0)
        curHealth = 0;

    if(curHealth > maxHealth)
        curHealth = maxHealth;

    if(maxHealth < 1)
        maxHealth = 1;

    healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
使用UnityEngine;
使用系统集合;
公共阶层的演员健康:单一行为{
公共int maxHealth=100;
公共卫生=100;
公众浮标长度;
//用于初始化
无效开始(){
healthBarLength=屏幕宽度/2;
}
//每帧调用一次更新
无效更新(){
AddjustCurrentHealth(0);
}
void OnGUI(){
GUI.Box(新的Rect(10,10,healthBarLength,20),curHealth+“/”+maxHealth);
}
public void AddjustCurrentHealth(int adj){
curHealth+=adj;
if(curHealth<0)
curHealth=0;
如果(curHealth>maxHealth)
curHealth=maxHealth;
如果(maxHealth<1)
maxHealth=1;
healthBarLength=(Screen.width/2)*(curHealth/(float)maxHealth);
}
}
敌人进攻

using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;

// Use this for initialization
void Start () {
    attackTimer = 0;
    coolDown = 2.0f;
}

// Update is called once per frame
void Update () {
    if(attackTimer > 0)
        attackTimer -= Time.deltaTime;

    if(attackTimer < 0)
        attackTimer = 0;

    if(attackTimer == 0) {
        Attack();
        attackTimer = coolDown;
    }
}

private void Attack() {
    float distance = Vector3.Distance(target.transform.position, transform.position);

    Vector3 dir = (target.transform.position - transform.position).normalized;

    float direction = Vector3.Dot(dir, transform.forward);

    if(distance < 2.5f) {
        if(direction > 0) {
            PlayersHealth eh = (PlayersHealth)target.GetComponent("PlayersHealth");
            eh.AddjustCurrentHealth(-10);
        }
    }
}
}
使用UnityEngine;
使用系统集合;
公共类攻击:单一行为{
公共游戏对象目标;
公共浮动攻击计时器;
公共浮子冷却;
//用于初始化
无效开始(){
攻击计时器=0;
冷却时间=2.0f;
}
//每帧调用一次更新
无效更新(){
如果(攻击计时器>0)
attackTimer-=Time.deltaTime;
如果(攻击计时器<0)
攻击计时器=0;
如果(攻击计时器==0){
攻击();
攻击计时器=冷却时间;
}
}
私有无效攻击(){
浮动距离=矢量3.距离(target.transform.position,transform.position);
Vector3 dir=(target.transform.position-transform.position).normalized;
浮点方向=矢量3.Dot(dir,transform.forward);
如果(距离<2.5f){
如果(方向>0){
PlayerHealth eh=(PlayerHealth)target.GetComponent(“PlayerHealth”);
eh.AddjustCurrentHealth(-10);
}
}
}
}
敌人的健康

Using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;

public float healthBarLength;

// Use this for initialization
void Start () {
    healthBarLength = Screen.width / 2;
}

// Update is called once per frame
void Update () {
    AddjustCurrentHealth(0);
}

void OnGUI() {
    GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);
}

public void AddjustCurrentHealth(int adj) {
    curHealth += adj;

    if(curHealth < 0)
        curHealth = 0;

    if(curHealth > maxHealth)
        curHealth = maxHealth;

    if(maxHealth < 1)
        maxHealth = 1;

    healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
使用UnityEngine;
使用系统集合;
公共卫生:单一行为{
公共int maxHealth=100;
公共卫生=100;
公众浮标长度;
//用于初始化
无效开始(){
healthBarLength=屏幕宽度/2;
}
//每帧调用一次更新
无效更新(){
AddjustCurrentHealth(0);
}
void OnGUI(){
GUI.Box(新的Rect(10,40,healthBarLength,20),curHealth+“/”+maxHealth);
}
public void AddjustCurrentHealth(int adj){
curHealth+=adj;
if(curHealth<0)
curHealth=0;
如果(curHealth>maxHealth)
curHealth=maxHealth;
如果(maxHealth<1)
maxHealth=1;
healthBarLength=(Screen.width/2)*(curHealth/(float)maxHealth);
}
}

我会喜欢任何人在这方面的专业知识,因为我在水平的最后阶段,这真的是付出了代价

为什么要用EnemyHealth的更新方法调用AddjustCurrentHealth每帧?只要修改EnemyHealth对象的maxHealth和curHealth值的运行状况的唯一方法是通过AddjustCurrentHealth方法(这意味着您应该使用私有setter将它们设置为属性),您就只需要在实际需要进行调整时调用它方法开始它们的动画。你为什么要使用“FactorySprite”,unity动画师很擅长2D,有很多教程,还有。。。它已经在unity中了……就像@eshs提到的那样,我看不到在您的应用程序中调用“sprite.Play()”
Using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;

public float healthBarLength;

// Use this for initialization
void Start () {
    healthBarLength = Screen.width / 2;
}

// Update is called once per frame
void Update () {
    AddjustCurrentHealth(0);
}

void OnGUI() {
    GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);
}

public void AddjustCurrentHealth(int adj) {
    curHealth += adj;

    if(curHealth < 0)
        curHealth = 0;

    if(curHealth > maxHealth)
        curHealth = maxHealth;

    if(maxHealth < 1)
        maxHealth = 1;

    healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}