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

Increased bullet speed hitting wrong point (weird issues)

Discussion in 'Physics' started by Denis-Valjean, Apr 10, 2016.

  1. Denis-Valjean

    Denis-Valjean

    Joined:
    Mar 25, 2015
    Posts:
    39
    Hi, I am testing a FPS using a ak47 gun model. Tried to replicate the real bullet speed of this gun. Found its around 700 m/s. The bullet fires from an empty object in front of the gun and has a rigibody. Measuring the speed with rigibody.velocity.magnitude.

    When using a 100m/s bullet speed, all bullets go exactly in the middle of crosshair, no problems. But when I increase the speed to 700m/s to match the real bullet speed, the impact points (I drawn a bullet hole at impact point) sometimes are far away from crosshair, sometimes a little close and a few times correct.

    Is this bullet speed too high for unity physics to deal with?? Any suggestions to solve this issue? I am a bit frustrated.. spent the hole night trying to solve it. Thanks in advance.
     
  2. KristoE

    KristoE

    Joined:
    Oct 13, 2012
    Posts:
    90
    I think we would need to see a bit of the code you are using to tell what is going wrong. You are saying that when you set the bullet speed to that high it will hit to the left,right,up or down of the target?
    Only thing I can think of as the cause is that you are setting the initial velocity incorrectly.
     
    Denis-Valjean likes this.
  3. Denis-Valjean

    Denis-Valjean

    Joined:
    Mar 25, 2015
    Posts:
    39
    Thanks Kristo. Yes, I am saying that at this speed the bullets hit the wrong place (bullet holes - impact points drawn left, up, right of the target) or even do not register any collision.

    1. This is method I use to fire the bullets. It is inside FixedUpdate():



    2. And this is the script attached to bullet Gameobject:

    Code (CSharp):
    1.     // Update is called once per frame
    2.     void Update ()
    3.     {
    4.         Ray raio = new Ray (transform.position, -transform.right);
    5.         Debug.DrawRay (transform.position, - transform.right * 20f, Color.blue);
    6.         Debug.DrawRay (transform.position, -transform.right * 20f, Color.magenta);
    7.  
    8.         RaycastHit impacto;
    9.         if (Physics.Raycast(raio.origin, -transform.right, out impacto, 1f))
    10.         {
    11.            
    12.         //    Destroy (gameObject);
    13.             gameObject.SetActive(false);
    14.             print ("Foi o Ray");
    15.             GameObject instaHole = Instantiate (bulletHole, impacto.point, Quaternion.FromToRotation(Vector3.up, impacto.normal)) as GameObject;
    16.             instaHole.transform.parent = impacto.transform; // coloca o buraco de bala como filho do objeto com o qual a bala colidiiu//
    17.         }
    18.  
    19.  
    20.  
    21.  
    22.     }
    23.  
     
  4. KristoE

    KristoE

    Joined:
    Oct 13, 2012
    Posts:
    90
    I don't quite understand what you are going for with this code, but it seems to me there are an abnormally high number of raycasts happening.

    Right now it seems to me that the issue is that the raycast distance from the bullet object is super short, I would suggest setting the max ray distance to atleast bulletVelocity.magnitude*Time.fixedDeltaTime.
    Also, switch the Update function to FixedUpdate.

    That way it will cast a ray in front of the bullet to see if it will hit the target during the next physics update.
    What I think is going wrong right now is that the bullet hits the wall and that stops its forward momentum, but not the slight sideways momentum it has, so it will slide along the wall until the raycast finally gets called.
     
    Denis-Valjean likes this.
  5. Denis-Valjean

    Denis-Valjean

    Joined:
    Mar 25, 2015
    Posts:
    39
    Thank you very much Kristo!!!! You were right!!!!! Set raycast from 1 to 5 units and changed the bullet script to fixedupdate. Everything working like a charm!!!!! Thank you so much!!!!!!
     
  6. KristoE

    KristoE

    Joined:
    Oct 13, 2012
    Posts:
    90
    You're welcome. Glad I could be helpful.