在 Unity 中设置技能

Setting Up Skill in Unity

过去一个月+ 我通过在 Unity.I 制作游戏学到了很多东西,这样做很有趣。但是有些事情仍然让我感到困惑。我正在尝试为角色设置一项技能,而且进展顺利。当角色施放技能时,技能在角色后面而不是前面。所以我想玩位置和轮换来让它工作但仍然没有。值得一提的是,预制件有它自己的动作。所以到目前为止我的代码是这样的。所以帮助会很大,一些关于技能系统背后的逻辑的教学将非常感激。那么来看看:

using UnityEngine;

public class MagicSkill : MonoBehaviour
{
    public GameObject hand; // Players right hand
    public GameObject fireballPrefab; // Fireball particle
    Vector3 fireballPos; // Adding the transform.position from the players hand
    Quaternion fireballRot; // Adding the rotation of the players hand
    private bool isPressed = false; //Skill button (mobile)
    public Animator animator; // Casting spell animation

void Update()
{
    fireballPos = hand.transform.position; // Getting the position of the hand for Instatiating
    fireballRot.x = hand.transform.rotation.x; // Getting the rotation of the hand for x Axis
    fireballRot.y = hand.transform.rotation.y; // Getting the rotation of the hand for y Axis (Here i try to modify the values but still nothing)
    fireballRot.z = hand.transform.rotation.z; // Getting the rotation of the hand for z Axis

    if (isPressed == true)
    {
        animator.SetBool("Magic", true);

        if (hand.transform.position.y >= 20) // Here I got the exact position of the hand for when to 
        Instatiate the skill
        {
            for (int i = 0; i < 1; i++) // For some reason it instatiates too many prefabs (I think it's because it's in Update method)
            {
                Instantiate(fireballPrefab, fireballPos, Quaternion.Euler(fireballRot.x, fireballRot.y, fireballRot.z));
                Invoke("Update", 2.0f); // I'm trying to slow down the pressed button so that it want spawn every frame
            }
        }
    }
    else
    {
        animator.SetBool("Magic", false);
    }
}

public void MagicSkills()
{
    if (isPressed == false)
    {
        isPressed = true;
    }
    else
    {
        isPressed = false;
    }
}
}

保留对角色变换的引用,并使用该变换来实例化火球,而不是使用手的旋转。手的旋转相对于 body 没有改变,所以这就是为什么球总是朝同一个方向移动的原因。

在尝试了一些代码之后,我设法找到了一个 solution.Here 我 post 我在 least.Maybe 的工作代码,它将帮助其他人 too.For 这个要正常工作,您必须从 button.Many 中删除事件触发器 OnPointerUp 感谢您的帮助 Guilherme Schaidhauer Castro

using UnityEngine;

public class MagicSkill : MonoBehaviour
{
public GameObject hand; // Players right hand
public GameObject fireballPrefab; // Fireball particle
public Animator animator; // Casting spell animation
Vector3 fireballPos; // Adding the transform.position from the players hand
private bool isPressed = false; //Skill button (mobile)

void Update()
{
    fireballPos = hand.transform.position; // Getting the position of the hand for Instatiating

    if (isPressed == true)
    {
        animator.SetBool("Magic", true);

        if (hand.transform.position.y >= 20) // Here I got the exact position of the hand for when to Instatiate the skill
        {
            Instantiate(fireballPrefab, fireballPos, Quaternion.identity);
            isPressed = false;
        }
    }
    else
    {
        animator.SetBool("Magic", false);
    }
}

public void MagicSkills()
{
    if (isPressed == false)
    {
        isPressed = true;
    }
    else
    {
        isPressed = false;
    }
}
}