Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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# 为什么赢了';我的骷髅不会对我的玩家造成伤害吗?_C#_Unity3d_2d_Game Engine - Fatal编程技术网

C# 为什么赢了';我的骷髅不会对我的玩家造成伤害吗?

C# 为什么赢了';我的骷髅不会对我的玩家造成伤害吗?,c#,unity3d,2d,game-engine,C#,Unity3d,2d,Game Engine,因此,每次骨架击中玩家/角色时。它不会显示“击中”这个词。我做错了什么 骷髅敌脚本 private void Start() { maxX = transform.position.x + (distance); minX = maxX - distance; //if (Random.value > 0.5) direction = 1; //else direction = -1; } 无效唤醒 void Awake() { a

因此,每次骨架击中玩家/角色时。它不会显示“击中”这个词。我做错了什么



骷髅敌脚本

private void Start()
{
    maxX = transform.position.x + (distance);
    minX = maxX - distance;

    //if (Random.value > 0.5) direction = 1;
    //else direction = -1;
}


无效唤醒

void Awake()
{
    anim = GetComponent<Animator>();
    playerPos = GameObject.Find("George").transform;
    myBody = GetComponent<Rigidbody2D>();
}
void Update()
{
    if (Vector3.Distance(transform.position, playerPos.position) <= 4.0f) patrol = false;
    else patrol = true;
}
private void FixedUpdate()
{
    if (anim.GetBool("Death"))
    {
        myBody.velocity = Vector2.zero;
        GetComponent<Collider2D>().enabled = false;
        myBody.isKinematic = true;
        anim.SetBool("Attack", false);
        return;
    }


    if (myBody.velocity.x > 0)
    {
        transform.localScale = new Vector2(1f, transform.localScale.y);
        anim.SetBool("Attack", false);
    }
    else if
        (myBody.velocity.x < 0) transform.localScale = new Vector2(-1f, transform.localScale.y);


    if (patrol)
    {
        detect = false;
        switch (direction)
        {
            case -1:
                if (transform.position.x > minX)
                    myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
                else
                    direction = 1;
                break;
            case 1:
                if (transform.position.x < maxX)
                    myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
                else
                    direction = -1;
                break;
        }
    }
    else
    {
        if (Vector2.Distance(playerPos.position, transform.position) >= 1.0f)
        {
            if (!detect)
            {
                detect = true;
                anim.SetTrigger("Detect");
                myBody.velocity = new Vector2(0, myBody.velocity.y);
            }
            if (anim.GetCurrentAnimatorStateInfo(0).IsName("Detect")) return;



            Vector3 playerDir = (playerPos.position - transform.position).normalized;

            if (playerDir.x > 0)
                myBody.velocity = new Vector2(moveSpeed + 0.4f, myBody.velocity.y);
            else
                myBody.velocity = new Vector2(-(moveSpeed + 0.4f), myBody.velocity.y);
        }
        else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
        {
            myBody.velocity = new Vector2(0, myBody.velocity.y);
            anim.SetBool("Attack", true);
        }
    }
}
 public int health = 100;

    void Awake()
    {

    }
 void Update()
    {
        if (health < 1)
        {
            print("Dead");
        }    
    }
 public void TakeDamage(int damage)
    {
        FindObjectOfType<CameraShake>().ShakeItMedium();
        health -= damage;
    }

    private void OnTriggerEnter2D(Collider2D target)
    {
        if(target.tag == "Fireball")
        {
            TakeDamage(25);
        }
    }
无效更新

void Awake()
{
    anim = GetComponent<Animator>();
    playerPos = GameObject.Find("George").transform;
    myBody = GetComponent<Rigidbody2D>();
}
void Update()
{
    if (Vector3.Distance(transform.position, playerPos.position) <= 4.0f) patrol = false;
    else patrol = true;
}
private void FixedUpdate()
{
    if (anim.GetBool("Death"))
    {
        myBody.velocity = Vector2.zero;
        GetComponent<Collider2D>().enabled = false;
        myBody.isKinematic = true;
        anim.SetBool("Attack", false);
        return;
    }


    if (myBody.velocity.x > 0)
    {
        transform.localScale = new Vector2(1f, transform.localScale.y);
        anim.SetBool("Attack", false);
    }
    else if
        (myBody.velocity.x < 0) transform.localScale = new Vector2(-1f, transform.localScale.y);


    if (patrol)
    {
        detect = false;
        switch (direction)
        {
            case -1:
                if (transform.position.x > minX)
                    myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
                else
                    direction = 1;
                break;
            case 1:
                if (transform.position.x < maxX)
                    myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
                else
                    direction = -1;
                break;
        }
    }
    else
    {
        if (Vector2.Distance(playerPos.position, transform.position) >= 1.0f)
        {
            if (!detect)
            {
                detect = true;
                anim.SetTrigger("Detect");
                myBody.velocity = new Vector2(0, myBody.velocity.y);
            }
            if (anim.GetCurrentAnimatorStateInfo(0).IsName("Detect")) return;



            Vector3 playerDir = (playerPos.position - transform.position).normalized;

            if (playerDir.x > 0)
                myBody.velocity = new Vector2(moveSpeed + 0.4f, myBody.velocity.y);
            else
                myBody.velocity = new Vector2(-(moveSpeed + 0.4f), myBody.velocity.y);
        }
        else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
        {
            myBody.velocity = new Vector2(0, myBody.velocity.y);
            anim.SetBool("Attack", true);
        }
    }
}
 public int health = 100;

    void Awake()
    {

    }
 void Update()
    {
        if (health < 1)
        {
            print("Dead");
        }    
    }
 public void TakeDamage(int damage)
    {
        FindObjectOfType<CameraShake>().ShakeItMedium();
        health -= damage;
    }

    private void OnTriggerEnter2D(Collider2D target)
    {
        if(target.tag == "Fireball")
        {
            TakeDamage(25);
        }
    }
无效更新

void Awake()
{
    anim = GetComponent<Animator>();
    playerPos = GameObject.Find("George").transform;
    myBody = GetComponent<Rigidbody2D>();
}
void Update()
{
    if (Vector3.Distance(transform.position, playerPos.position) <= 4.0f) patrol = false;
    else patrol = true;
}
private void FixedUpdate()
{
    if (anim.GetBool("Death"))
    {
        myBody.velocity = Vector2.zero;
        GetComponent<Collider2D>().enabled = false;
        myBody.isKinematic = true;
        anim.SetBool("Attack", false);
        return;
    }


    if (myBody.velocity.x > 0)
    {
        transform.localScale = new Vector2(1f, transform.localScale.y);
        anim.SetBool("Attack", false);
    }
    else if
        (myBody.velocity.x < 0) transform.localScale = new Vector2(-1f, transform.localScale.y);


    if (patrol)
    {
        detect = false;
        switch (direction)
        {
            case -1:
                if (transform.position.x > minX)
                    myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
                else
                    direction = 1;
                break;
            case 1:
                if (transform.position.x < maxX)
                    myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
                else
                    direction = -1;
                break;
        }
    }
    else
    {
        if (Vector2.Distance(playerPos.position, transform.position) >= 1.0f)
        {
            if (!detect)
            {
                detect = true;
                anim.SetTrigger("Detect");
                myBody.velocity = new Vector2(0, myBody.velocity.y);
            }
            if (anim.GetCurrentAnimatorStateInfo(0).IsName("Detect")) return;



            Vector3 playerDir = (playerPos.position - transform.position).normalized;

            if (playerDir.x > 0)
                myBody.velocity = new Vector2(moveSpeed + 0.4f, myBody.velocity.y);
            else
                myBody.velocity = new Vector2(-(moveSpeed + 0.4f), myBody.velocity.y);
        }
        else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
        {
            myBody.velocity = new Vector2(0, myBody.velocity.y);
            anim.SetBool("Attack", true);
        }
    }
}
 public int health = 100;

    void Awake()
    {

    }
 void Update()
    {
        if (health < 1)
        {
            print("Dead");
        }    
    }
 public void TakeDamage(int damage)
    {
        FindObjectOfType<CameraShake>().ShakeItMedium();
        health -= damage;
    }

    private void OnTriggerEnter2D(Collider2D target)
    {
        if(target.tag == "Fireball")
        {
            TakeDamage(25);
        }
    }
void Update()
{
如果(健康状况<1)
{
印刷品(“死亡”);
}    
}
无效接受伤害

void Awake()
{
    anim = GetComponent<Animator>();
    playerPos = GameObject.Find("George").transform;
    myBody = GetComponent<Rigidbody2D>();
}
void Update()
{
    if (Vector3.Distance(transform.position, playerPos.position) <= 4.0f) patrol = false;
    else patrol = true;
}
private void FixedUpdate()
{
    if (anim.GetBool("Death"))
    {
        myBody.velocity = Vector2.zero;
        GetComponent<Collider2D>().enabled = false;
        myBody.isKinematic = true;
        anim.SetBool("Attack", false);
        return;
    }


    if (myBody.velocity.x > 0)
    {
        transform.localScale = new Vector2(1f, transform.localScale.y);
        anim.SetBool("Attack", false);
    }
    else if
        (myBody.velocity.x < 0) transform.localScale = new Vector2(-1f, transform.localScale.y);


    if (patrol)
    {
        detect = false;
        switch (direction)
        {
            case -1:
                if (transform.position.x > minX)
                    myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
                else
                    direction = 1;
                break;
            case 1:
                if (transform.position.x < maxX)
                    myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
                else
                    direction = -1;
                break;
        }
    }
    else
    {
        if (Vector2.Distance(playerPos.position, transform.position) >= 1.0f)
        {
            if (!detect)
            {
                detect = true;
                anim.SetTrigger("Detect");
                myBody.velocity = new Vector2(0, myBody.velocity.y);
            }
            if (anim.GetCurrentAnimatorStateInfo(0).IsName("Detect")) return;



            Vector3 playerDir = (playerPos.position - transform.position).normalized;

            if (playerDir.x > 0)
                myBody.velocity = new Vector2(moveSpeed + 0.4f, myBody.velocity.y);
            else
                myBody.velocity = new Vector2(-(moveSpeed + 0.4f), myBody.velocity.y);
        }
        else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
        {
            myBody.velocity = new Vector2(0, myBody.velocity.y);
            anim.SetBool("Attack", true);
        }
    }
}
 public int health = 100;

    void Awake()
    {

    }
 void Update()
    {
        if (health < 1)
        {
            print("Dead");
        }    
    }
 public void TakeDamage(int damage)
    {
        FindObjectOfType<CameraShake>().ShakeItMedium();
        health -= damage;
    }

    private void OnTriggerEnter2D(Collider2D target)
    {
        if(target.tag == "Fireball")
        {
            TakeDamage(25);
        }
    }
public void TakeDamage(内部损坏)
{
FindObjectOfType().ShakeItMedium();
健康-=损害;
}
私有无效OnTiggerEnter2D(碰撞R2D目标)
{
如果(target.tag==“Fireball”)
{
损伤(25);
}
}

虽然这一长堆代码可能还有许多其他问题:

if(attackPlayer==null)
{
如果(attackPlayer.tag==“Player”)
{
打印(“点击!”);
attackPlayer.gameObject.GetComponent().TakeDamage(伤害);
}
}


您检查attackPlayer是否为空,然后尝试对其执行操作。。。我很确定你的意思是
如果(attackPlayer!=null)
..

那么长的代码堆可能还有很多其他问题:

if(attackPlayer==null)
{
如果(attackPlayer.tag==“Player”)
{
打印(“点击!”);
attackPlayer.gameObject.GetComponent().TakeDamage(伤害);
}
}

您检查attackPlayer是否为空,然后尝试对其执行操作。。。我很确定你的意思是
如果(attackPlayer!=null)

你的脚本中似乎没有调用“攻击”方法

else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
{
    myBody.velocity = new Vector2(0, myBody.velocity.y);
    // calling the attack method, so the physics cast is being made
    Attack();
    anim.SetBool("Attack", true);
}
else if(Vector2.Distance(playerPos.position,transform.position)脚本中似乎没有调用“攻击”方法

else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
{
    myBody.velocity = new Vector2(0, myBody.velocity.y);
    // calling the attack method, so the physics cast is being made
    Attack();
    anim.SetBool("Attack", true);
}

else if(Vector2.Distance(playerPos.position,transform.position)请将此转换为一个函数,这里有很多代码,而不是“它不工作”有太多的内容供人们浏览,而链接通常不太受欢迎。我对这个页面还是新手,但我会尝试这样做的!:请将此转换为一个页面,那里有很多代码,而不是“它不起作用”有太多的内容供人们浏览,而且链接通常不太受欢迎。我还是这个页面的新手,但我会尝试这样做的!:我尝试将“!=”添加到攻击if语句中,但由于某些原因,它仍然不起作用。你从何处触发攻击功能?我尝试添加“!=”攻击if语句,但由于某种原因,它仍然不起作用。您从何处触发攻击函数