Home > Enterprise >  3D Isometric game in Unity - Projectile follows player movements
3D Isometric game in Unity - Projectile follows player movements

Time:05-08

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

CodePudding user response:

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)
  • Related