-1

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;
    }
}
}
  • Do you mean that the fireball is in the wrong position, or do you mean that the hand is being drawn in front of the fireball so that you can't see the fireball? – Ben Rubin Jan 12 '21 at 21:52
  • when the fireball is instantiated it plays it's animation and goes behind the character and keeps going.But when instatiated it should go in front of the character –  Jan 12 '21 at 23:01
  • 1
    Can you show a picture of the problem? And how are you moving the fireball? Are you applying a force to a rigidbody or are you updating its position during `Update`? – Ben Rubin Jan 12 '21 at 23:04
  • i'm about to post it.the fireball is moving by it's motion only as i stated in the comment above. –  Jan 12 '21 at 23:12
  • I'm ulploading a small video to youtube now.I'll post the link here. –  Jan 12 '21 at 23:17
  • https://youtu.be/eW5rF6KqQe8 look here –  Jan 12 '21 at 23:27
  • Instead of using `Quaterenion.Euler(fireballRot.x...`, what happens if you use `hand.transform.rotation` – Ben Rubin Jan 12 '21 at 23:34
  • the same thing.By the way i'm moving the fireball with this code public float speed = 0.1f; void Update() { transform.Translate(Vector3.forward * speed); } –  Jan 12 '21 at 23:35
  • How are you moving the fireball? Objects don't have "motion" built-in, so you must be adding either a component or some code that's causing it to move. Maybe add a screenshot of the Inspector for your fireball prefab to your question. – Ben Rubin Jan 12 '21 at 23:40
  • look the motion code i sent above –  Jan 12 '21 at 23:41
  • It doesn't go "Behind" the character. It's just always moving in the same direction. Likely an issue with the `fireballRot`. Would be helpful to debug and see what values you are getting here when you instantiate the fireball – Guilherme Schaidhauer Castro Jan 12 '21 at 23:45
  • give a 2 minutes and i'll post a picture –  Jan 12 '21 at 23:47
  • Are you moving the hand? That code is instantiating a fireball at the location of the hand during each iteration of the `Update` as long as `isPressed` is true, but there's nothing in your code that is causing the fireball to move once it is instantiated. – Ben Rubin Jan 12 '21 at 23:47
  • https://prnt.sc/wm6lfi –  Jan 12 '21 at 23:59
  • this is the motion code i'm using https://prnt.sc/wm6pej –  Jan 13 '21 at 00:07
  • Why is this tagged `particles`? – derHugo Jan 13 '21 at 12:45
  • Because it's referring to a particle prefab. –  Jan 13 '21 at 12:48

2 Answers2

1

After some playing around with the code I managed to find a solution.Here I post the working code for me at least.Maybe it will help someone else too.For this to work properly you must remove the Event Trigger OnPointerUp from your button.Many thanks for the help 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;
    }
}
}
0

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.

  • I thought about that and the thing is that i want to achieve the effect of the fireball moving from the hand of the character. –  Jan 13 '21 at 00:04
  • or you suggest that i keep only the refernce of y Axis of the character? –  Jan 13 '21 at 00:05
  • You can use the position of the hand and the rotation of the character. – Guilherme Schaidhauer Castro Jan 13 '21 at 00:53
  • i'll try it tommorow.Now it's time to sleep.Past 03:00 here.thanks for advice.Will tell the results tommorow. –  Jan 13 '21 at 01:00
  • thanks mate.it's working.Now i need to find a way to correct the script so i can Instanciate only 1 firebal and not many.Mind to help on that too? –  Jan 13 '21 at 08:24
  • Sure. Could you raise a separate question for that? I'll check it then – Guilherme Schaidhauer Castro Jan 13 '21 at 12:18
  • well i tried to ask a new question and it seems that i'm banned.Dunno what to to now. –  Jan 13 '21 at 12:42
  • you probably just need to set 'isPressed = false' right after the 'if (isPressed == true)' to make sure that this same code doesn't get called immediately again and remove the for loop 'for (int i = 0; i < 1; i++)' – Guilherme Schaidhauer Castro Jan 13 '21 at 13:02
  • I'll try it now.It seems that it will work.It never crossed my mind to do that.Thanks. –  Jan 13 '21 at 13:08
  • well it does instatiate once in onPointerDown and once in OnPointerUp –  Jan 13 '21 at 13:16