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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

I have no background in trigonometry. Why does my projectiles lose velocity when my player move?

Discussion in 'Scripting' started by xoXpandabearXox, Aug 27, 2019.

  1. xoXpandabearXox

    xoXpandabearXox

    Joined:
    May 25, 2017
    Posts:
    15
    Code (CSharp):
    1.  void RadialFire(float _numberOfProjectiles )
    2.     {
    3.    
    4.  
    5.         float angleStep = possibleFloatAngle / _numberOfProjectiles;
    6.         float angle = 0f;
    7.  
    8.         for (int i = 0; i <= _numberOfProjectiles - 1; i++)
    9.         {
    10.             // Direction calculations.
    11.             float projectileDirXPosition = startPoint.x + Mathf.Sin((angle * Mathf.PI) / 180) * radius;
    12.             float projectileDirYPosition = startPoint.y + Mathf.Cos((angle * Mathf.PI) / 180) * radius;
    13.  
    14.             // Create vectors.
    15.             Vector3 projectileVector = new Vector3(projectileDirXPosition, projectileDirYPosition, 0);
    16.             Vector3 projectileMoveDirection = (projectileVector - startPoint).normalized * projectileSpeed;
    17.  
    18.             // Create game objects.
    19.             GameObject tmpObj = Instantiate(projectile.gameObject, startPoint, Quaternion.identity);
    20.             tmpObj.GetComponent<Rigidbody>().velocity = new Vector3(projectileMoveDirection.x, 0, projectileMoveDirection.y);
    21.  
    22.             // Destory the gameobject after 10 seconds.
    23.             Destroy(tmpObj, 5);
    24.  
    25.             angle += angleStep;
    26.  
    27.         }
    28.  
    29.  
    30.     }
    startPoint = transform.position;

    I needed a radial projectile spawner. When i move forward or backwards from the center. my projectiles velocity is slower. Usually i like to figure things out for myself, but theirs no way i could possibly figure this out on my own.

    Heres a link to the video i watched
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Other than the typo and wrong value in the comment in line 22, i dont see anything wrong with this code.
    Are you sure the projectile actually loses speed? If you move in the same direction as the projectile they will obviously appear slower. If they actually lose speed that would have to be caused by projectileMoveDirection getting smaller, but since it's a normalized vector and projectileSpeed should not change, that does not seem likely. I cant test it myself right now. Are existing projectiles getting slower when you move, or only ones you spawn after you started moving? And are you sure they actually decrease their velocity?
    To test this you could keep track of the last bullet and print its velocity to the console each frame.

    Also, i find it infuriating how one character of your name gets pushed in a new line :(
     
  3. xoXpandabearXox

    xoXpandabearXox

    Joined:
    May 25, 2017
    Posts:
    15
    Sorry about the username, If only i had removed the o's. Well too answer your question. Are existing projectiles getting slower when you move, or only ones you spawn after you started moving? When the player is position at (0 , 0 , 0) the projectiles shoots out fast. When i move forward or negative on the z axis then stop the player, and then fire my projectiles the projectiles move very slowly.Edit the projectiles shoot out slowly and continue at a slow pace
     
    Last edited: Aug 27, 2019
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Haha dont worry about it, that was more of a joke on my end ;)

    I believe i know what the problem is. Two actually.
    Imagine projectileVector ends up being something like (1,0,0). And you change your position to (0,0,-1), then that would make ((1,0,0) - (0,0,-1)).normalized = (0.5, 0, 0.5). This means we changed the direction based on our position. I'm not sure if that makes sense. So first of all, i'm pretty sure you can delete the startPosition part from that equation. I'm pretty sure that should be your velocity, not position. Changing the proportional velocity (and thus the speed) based on your speed seems reasonable - so projectiles fly faster when you run in the same direction they are fired, and slower vice versa.

    Secondly and more importantly tho, in line 20 you create new Vector3(projectileMoveDirection.x, 0, projectileMoveDirection.y), but i'm pretty sure you mean to use .z instead of .y there. This would also explain why your projectiles end up slower. As described in the above example, moving your character changes the direction (not the speed!) but that means less speed per axis (x drops from 1 to 0.5). If you now skip to add the other 0.5 force on the z axis (because you use y, which is 0) the projectile obviously ends up getting slower.

    Hope this helps now :)
     
  5. xoXpandabearXox

    xoXpandabearXox

    Joined:
    May 25, 2017
    Posts:
    15
    Thank you so much. trying your fix right as we speak. I'll let you know how it goes
     
  6. xoXpandabearXox

    xoXpandabearXox

    Joined:
    May 25, 2017
    Posts:
    15
    OMG it worked like a charm. Not sure if you'll read this but THANK YOU AGAIN:). I'll post the edited code here, and on youtube so it will hopefully help someone else.

    Code (CSharp):
    1.  void RadialFire(float _numberOfProjectiles )
    2.     {
    3.  
    4.         float angleStep = possibleFloatAngle / _numberOfProjectiles;
    5.         float angle = 0f;
    6.  
    7.         for (int i = 0; i <= _numberOfProjectiles - 1; i++)
    8.         {
    9.             // Direction calculations.
    10.             float projectileDirXPosition =  Mathf.Sin((angle * Mathf.PI) / 180) * radius;
    11.             float projectileDirZPosition =  Mathf.Cos((angle * Mathf.PI) / 180) * radius;
    12.  
    13.             // Create vectors.
    14.             Vector3 projectileVector = new Vector3(projectileDirXPosition, 0, projectileDirZPosition);
    15.             Vector3 projectileMoveDirection = (projectileVector) * projectileSpeed;
    16.  
    17.             // Create game objects.
    18.             GameObject tmpObj = Instantiate(projectile.gameObject, muzzle.position, Quaternion.identity);
    19.             tmpObj.GetComponent<Rigidbody>().velocity = new Vector3(projectileMoveDirection.x, 0, projectileMoveDirection.z);
    20.  
    21.             // Destory the gameobject after 100,000,000,000 seconds :P.
    22.             Destroy(tmpObj, 5);
    23.  
    24.             angle += angleStep;
    25.  
    26.         }
    27.  
    28.  
    29.     }
     
    Last edited: Aug 27, 2019
  7. rj191951

    rj191951

    Joined:
    Oct 30, 2018
    Posts:
    11
    There is no need to normalize projectileVector, if you do not multiply sin() & cos() with radius value.

    You simply can remove radius value
     
  8. xoXpandabearXox

    xoXpandabearXox

    Joined:
    May 25, 2017
    Posts:
    15
    Thanks for the tip. i shall remove.