Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Problem when spawning my AI

Discussion in 'Scripting' started by jayjaynator, Apr 12, 2018.

  1. jayjaynator

    jayjaynator

    Joined:
    Dec 9, 2017
    Posts:
    3
    Hi guys!

    This is my first time on this forum.
    I am creating a script to drop a turret so it can kill ennemy. My turret aim ennemy and kill them, all fine.
    But when I add the TurretManager script, the turret are still fine if they are put in the scene as a prefab, but not when I spawn them with my player by pressing "e".

    The turret that is spawned is aiming at the enemy, but the bullet are always going in the same direction no matter where my ennemy is (the canon follow the enmy, but bullet still going in the Z direction aka forward).

    I think it has to do something about my Bullet script and the forward instructions, can somoene confirm and help me?
     

    Attached Files:

    Last edited: Apr 12, 2018
  2. jayjaynator

    jayjaynator

    Joined:
    Dec 9, 2017
    Posts:
    3
    Okey, something weird I noticed. The first half fractions of a seconds the bullets are going in the direction of the ennemy if I had them in the scene manualy while Unity is running, but after it goes back to "forward".
     
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    At a glance it looks like what's happening is when your bullet is instantiated it's being instantiated at a rotation of Quaternion.identity which is going to be Vector3.zero. Then on the bullet the Start function runs which adds force to it in the relative direction of its rotation of Vector3.zero. Then, back in the turret script immediately after the instantiation of the bullet the rotation of the bullet is set, but it's already got force on it going along the Vector3.zero forward direction so it's not going the right way. Instead of setting rotation after instantiating the bullet set it during the instantiation with Quaternion.Euler (bulletSpawnPoint.transform.rotation).

    I haven't tested it and could be way off the mark but hopefully that fixes the issue for you.
     
    jayjaynator likes this.
  4. jayjaynator

    jayjaynator

    Joined:
    Dec 9, 2017
    Posts:
    3
    Hello Quinn.

    I am quite busy so I am gonna explain later today how I solved my problem.
    But in short, it works now thanks to your comment. Gonna explain later how. :)