Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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_Particles - Fatal编程技术网

C# 在团结中建立技能

C# 在团结中建立技能,c#,unity3d,particles,C#,Unity3d,Particles,在过去的一个月里,我通过在Unity中制作一个游戏学到了很多东西。这样做我很开心。但有些事情仍然让我困惑。我正在尝试为角色设置一项技能,而且进展很顺利。当角色施放技能时,技能会在角色后面而不是前面。所以我想用位置和旋转来让它发挥作用,但还是没有。值得一提的是,预制板有自己的运动。到目前为止,我的代码是这样的。因此,帮助将是巨大的,一些关于技能体系背后的逻辑的教学将非常感激。因此,请看一看: using UnityEngine; public class MagicSkill : MonoBeh

在过去的一个月里,我通过在Unity中制作一个游戏学到了很多东西。这样做我很开心。但有些事情仍然让我困惑。我正在尝试为角色设置一项技能,而且进展很顺利。当角色施放技能时,技能会在角色后面而不是前面。所以我想用位置和旋转来让它发挥作用,但还是没有。值得一提的是,预制板有自己的运动。到目前为止,我的代码是这样的。因此,帮助将是巨大的,一些关于技能体系背后的逻辑的教学将非常感激。因此,请看一看:

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;
    }
}
}
使用UnityEngine;
公共类MagicSkill:单行为
{
公共游戏对象手;//玩家右手
公共游戏对象火球预制;//火球粒子
Vector3 fireballPos;//从玩家手上添加transform.position
四元数fireballRot;//添加玩家手的旋转
private bool isPressed=false;//技能按钮(移动)
公共动画师动画师;//施法动画
无效更新()
{
fireballPos=hand.transform.position;//获取手的位置以进行安装
fireballRot.x=hand.transform.rotation.x;//获取手的x轴旋转
fireballRot.y=hand.transform.rotation.y;//获取y轴的手的旋转(这里我尝试修改值,但仍然没有任何结果)
fireballRot.z=hand.transform.rotation.z;//获取手的z轴旋转
如果(isPressed==true)
{
动画师。SetBool(“魔法”,真实);
if(hand.transform.position.y>=20)//在这里,我得到了手的确切位置,以便在
恢复技能
{
for(int i=0;i<1;i++)//出于某种原因,它设置了太多的预置(我认为这是因为它在Update方法中)
{
实例化(FireballPrefact、fireballPos、四元数.Euler(fireballRot.x、fireballRot.y、fireballRot.z));
Invoke(“Update”,2.0f);//我试图减慢按下的按钮的速度,以便它希望生成每一帧
}
}
}
其他的
{
动画师。SetBool(“魔法”,假);
}
}
公共无效魔法技能()
{
如果(isPressed==false)
{
isPressed=true;
}
其他的
{
isPressed=false;
}
}
}

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

在对代码进行了一些尝试后,我设法找到了一个解决方案。在这里,我至少为我发布了工作代码。也许它也会帮助其他人。为了使它正常工作,您必须从按钮上移除事件触发器OnPointerUp。非常感谢您的帮助Guillerme Schaidhauer Castro


你是说火球放错位置了,或者你的意思是在火球前面画手,这样你就看不到火球了吗?当火球被实例化时,它会播放动画并在角色后面继续。但是当它被稳定时,它应该在角色前面。你能显示问题的图片吗?你是怎么移动火球的?你是在向刚体施加力还是在更新过程中更新刚体的位置?我正要发布它。火球只是按照我在上面的评论中所说的运动。我现在正在向youtube上传一个小视频。我将在这里发布链接。我考虑过这一点,我想实现火球从角色的手上移动。或者你建议我只保留角色y轴的参考?你可以使用手的位置和角色的旋转。我明天试试。现在该睡觉了。这里已经过了03:00。谢谢你的建议。明天会告诉结果的。谢谢,伙计。它正在工作。现在我需要找到一个方法为了更正脚本,我只能实例化1个firebal,而不是很多。介意帮忙吗?
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;
    }
}
}