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. Dismiss Notice

Question How to make a projectile spreading effect for topdown?

Discussion in 'Scripting' started by billygamesinc, Jul 27, 2023.

  1. billygamesinc

    billygamesinc

    Joined:
    Dec 5, 2020
    Posts:
    245
    upload_2023-7-27_16-0-7.png

    In the context of a topdown shooter. The origin of where the projectiles are determined. Ideally I would like to not have to use rotations via eulers but am willing to if need be. Im struggling with two things at the moment.
    1. Creating a formula which can deal with spread factor based on the amount of projectiles as displayed above. If it is an odd number, the first bullet should start in the middle. If it's not an odd number the bullet should start at half of what the spread factor is from the top. That's my logic at the moment but all of them come out at the same time. Maybe its better to look into using Unity's Particle system for this instead? I plan on doing more than just bullets with the projectiles though like homing missiles,lasers and pools of lava.
    2. Determining how they move without forcing a rotation. The easiest way is to make it rotate in the direction its moving and just make it go transform.right but I was wondering if theres a way to just make it move in a set direction.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Multiply Vector3s by a Quaternion to rotate them.

    Code (csharp):
    1. // bend it 45 degrees around +Z vector
    2. Vector3 bentVector = Quaternion.Euler( 0, 0, 45) * originalVector;
    EDIT: here's more of an example going around a full circle:

    https://github.com/kurtdekker/makeg...ProcGenStuff/SpawnInACircle/SpawnInACircle.cs

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo
     
    billygamesinc likes this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1.     void SpawnBullets(int count)
    2.     {
    3.         for (int i=0;i<count;i++)
    4.         {
    5.             Quaternion startRot=Quaternion.AngleAxis((-10*(count-1)+(i*20)),Vector3.up);
    6.             GameObject b=Instantiate(bulletPrefab,transform.position,startRot*transform.rotation);
    7.             b.GetComponent<Rigidbody>().AddForce(b.transform.forward*10,ForceMode.Impulse);
    8.         }
    9.     }
     
  4. billygamesinc

    billygamesinc

    Joined:
    Dec 5, 2020
    Posts:
    245
    thank you ill try this out
     
  5. billygamesinc

    billygamesinc

    Joined:
    Dec 5, 2020
    Posts:
    245
    yes this is using transform.forward though i would like to control the direction rather than making it go off its rotation.