Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

c# - Setting Up Skill in Unity

These past month+ I learned many things by making a game in Unity.I have a lot of fun doing so. But some thing are still confusing me. I'm trying to setup a skill to the character and it goes almost well. When the character is casting the skill, the skill goes behind the character and not in front. So i thought to play with positions and rotations to make it work but still nothing. Worth to mention that the prefab has it's own motion. So my code so far is this. So help would be great and some teaching about the logic behind the skills system would be much appreciated. So take a look:

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;
    }
}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Keep a reference to the character's transform and use that transform to instantiate the fireball instead of using the rotation from the hand. The rotation of the hand is not changing in relation to the body, so that's why the ball is always going in the same direction.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...