0

I'm trying to make a 3D Isometric game with a wizard shooting fireballs. I managed to make it shoot the fireball but they go in the direction which the wizard is facing: if I rotate the wizard the fireballs change direction. What can I do? Thanks for helping me. This is the script I made (attached to the player):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WizardController : Characters
{
    [SerializeField]
    public Transform spawnMagic;

    private GameObject magicShot;
    public List<GameObject> magicBullets = new List<GameObject>();

    private void Start()
    {
        maxHP = 150.0f;
        magicShot = magicBullets[0];
    }

    void Update()
    {
        GetInputs();
        Attack();
        Defend();
        cameraFollow();
    }

    private void FixedUpdate()
    {
        LookAt();
        Move();
    }

    public override void Attack()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            GameObject magicBullet;

            isAttacking = true;
            GetComponent<Animator>().SetBool("hit1", true);

            if (spawnMagic != null)
            {
                magicBullet = Instantiate(magicShot, spawnMagic.transform.position, Quaternion.identity);
            }
        }
        else
        {
            GetComponent<Animator>().SetBool("hit1", false);
        }
    }
}

The movement script for the bullet is a simple "transform.position" line:

transform.position += spawnMagic.forward * (speed * Time.deltaTime);

And this is what happen when the player shoot:

https://youtu.be/TYwWDr8W4Q4

1 Answers1

0

To solve this problem, you must make the bullet movement independent of the any objects that are Child of wizard or depend on it transfrom. If you are careful, the spawnMagic rotates as the wizard moves, and the bullet is referenced by spawnMagic.forward.

First you need to place bullet rotation same as spawn spawnMagic rotation during production.

magicBullet = Instantiate(magicShot, spawnMagic.transform.position, spawnMagic.transform.rotation)

Then replace spawnMagic.forward with local bullet forward at movement part, it will make bullet movement indepent of spawnMagic direction during move phase:

 transform.position += transform.forward * (speed * Time.deltaTime)
KiynL
  • 4,097
  • 2
  • 16
  • 34
  • Nope. Same result. This script is attached to the player, so that way it takes the rotation of the player. But the point is not the rotation, is the direction of the projectile: it moves toward the direction where the player is facing for the entire duration of its movements, so if the player shoot and then rotate the character, also the bullet rotate but since it's moving it turn toward the direction faced by the player. – NyctalusNoctula May 05 '22 at 21:42