最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

My animations are breaking in Unity using C# - Stack Overflow

programmeradmin1浏览0评论

Using this code:

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

public class PlayerCombat : MonoBehaviour
{
    public Animator animator;

    public Transform attackPoint;
    public float attackRange = 0.5f;
    public LayerMask enemyLayers;

    public int attackDamage = 40;

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

    void Attack()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            animator.SetTrigger("Attack");

            Collider[] hitEnemies = Physics.OverlapSphere(attackPoint.position, attackRange, enemyLayers);

            foreach (Collider enemy in hitEnemies)
            {
                enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
            }
        }

        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            animator.SetBool("Block", true);
        }
        else if (Input.GetKeyUp(KeyCode.Mouse1))
        {
            animator.SetBool("Block", false);
        }
    }

    void OnDrawGizmosSelected()
    {
        if (attackPoint == null)
            return;

        Gizmos.DrawWireSphere(attackPoint.position, attackRange);
    }
}

I'm trying to trigger animations. Ignore the Gizmos and the enemy parts just the animator stuff. In unity using this:

AnimationWindow

When I make transitions from "Any State" these two some thing breaks and I can only do the attack animation while holding down block. I want to know how to fix this.

Using this code:

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

public class PlayerCombat : MonoBehaviour
{
    public Animator animator;

    public Transform attackPoint;
    public float attackRange = 0.5f;
    public LayerMask enemyLayers;

    public int attackDamage = 40;

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

    void Attack()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            animator.SetTrigger("Attack");

            Collider[] hitEnemies = Physics.OverlapSphere(attackPoint.position, attackRange, enemyLayers);

            foreach (Collider enemy in hitEnemies)
            {
                enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
            }
        }

        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            animator.SetBool("Block", true);
        }
        else if (Input.GetKeyUp(KeyCode.Mouse1))
        {
            animator.SetBool("Block", false);
        }
    }

    void OnDrawGizmosSelected()
    {
        if (attackPoint == null)
            return;

        Gizmos.DrawWireSphere(attackPoint.position, attackRange);
    }
}

I'm trying to trigger animations. Ignore the Gizmos and the enemy parts just the animator stuff. In unity using this:

AnimationWindow

When I make transitions from "Any State" these two some thing breaks and I can only do the attack animation while holding down block. I want to know how to fix this.

Share Improve this question edited Feb 6 at 21:01 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 6 at 20:44 Justus TateJustus Tate 32 bronze badges 3
  • whats the conditions on those transitions – BugFinder Commented Feb 6 at 20:50
  • the default ones, but only exit time between swing and idle – Justus Tate Commented Feb 6 at 20:53
  • well there are no default conditions, so, thats confusing in its own right. – BugFinder Commented Feb 6 at 22:40
Add a comment  | 

1 Answer 1

Reset to default -1

From the code perspective, this looks correct. From understanding your code behaviour, you are update the state of the animation's "Block" value when the user is holding the mouse down or not.

The problem appears to be in your Animator's controller; without further context on what your animation tree looks like, and any parameters you have setup, it will be difficult to answer this question correctly.

My advice would be to check whether there's a condition assigned to your animation clip tree.

发布评论

评论列表(0)

  1. 暂无评论