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. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Bullet flies too slow? wtf!

Discussion in 'Physics' started by AdmiralFick, Jul 29, 2021.

  1. AdmiralFick

    AdmiralFick

    Joined:
    Apr 30, 2020
    Posts:
    18
    Hi,

    i try a little shooting android-game (3D), where the player stands on a fixed position and stuff appears, which the player should tap on to shoot at. I instantiate a bullet at my bullet-spawner's position, when the player taps on the screen, and get the direction to shoot to:

    Code (CSharp):
    1. if (Input.touchCount > 0) {
    2.              if (Input.GetTouch (0).phase == TouchPhase.Began) {
    3.                 Ray r = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    4.                 Debug.DrawRay(r.origin, r.direction * 10, Color.cyan);
    5.  
    6.                 GameObject bullet = Instantiate (bulletPrefab, shotSpawner.transform.position, Quaternion.identity)as GameObject;
    7.                 bullet.GetComponent<Rigidbody> ().velocity = r.direction * bulletSpeed;
    8.              }
    9.          }
    My problem is, that even when i set bulletSpeed to 50000f, the bullet flies way too slow .. how can i solve this?
     
    Last edited: Jul 29, 2021
  2. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Can you show the bullet gameobject inspector?

    It seams like you have a lot of drag
     
    AdmiralFick likes this.
  3. AdmiralFick

    AdmiralFick

    Joined:
    Apr 30, 2020
    Posts:
    18
     
  4. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    660
    Maybe the magnitude of r is too small. Anyways you're doing it wrong, if you want the bullets to fly exactly at bulletSpeed, you must normalize r before the multiplication, like this: .velocity = r.normalized*bulletSpeed.
     
    AdmiralFick likes this.
  5. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    I dont see anithing strange. It will be a coding error.
    Btw it will be a good idea to activate interpolation to the rigidbody to make the movement smoother.
     
    AdmiralFick likes this.
  6. AdmiralFick

    AdmiralFick

    Joined:
    Apr 30, 2020
    Posts:
    18

    I dont't know whats is going on, but the next day I opened unity and run the game, the bullets fly with lightspeed.. i normalized it anyway and not they go fast.. .but .. now I have a other problem with this:

    there are walls and boxed on my playground and when the bullet collides with something, it instantiates a little explosion-gameobject and destroys itself. now the bullets fly so fast, that if a bullet collides with a wall, the instantiated explosion is beeing instantiated behind the wall.. like the script, which instantiates the explosion is beeing executed when the bullet's position would be behind the wall..

    when i reduce the bullet-speed, the explosion is where it should be, on the surface of the wall.. but it looks not good because it flies to slow.