问题描述
过去一个月+我通过在 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;
}
}
}
解决方法
在对代码进行一些尝试后,我设法找到了解决方案。在这里,我至少为我发布了工作代码。也许它也会对其他人有所帮助。为了使其正常工作,您必须从您的按钮。非常感谢您的帮助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;
}
}
}
,
保留对角色变换的引用,并使用该变换来实例化火球,而不是使用手的旋转。手的旋转相对于身体没有变化,所以这就是为什么球总是朝着同一个方向前进。