Search Unity

Aiming, Shooting from Gun, Bullet Trajectory - 1st and 3rd person shooters. Need Help!!

Discussion in 'Physics' started by Denis-Valjean, Jan 3, 2016.

  1. Denis-Valjean

    Denis-Valjean

    Joined:
    Mar 25, 2015
    Posts:
    39
    Hi guys,

    I am an ambitious person and doing a prototype of a 1st and 3rd person shooter, like, for instance, Star Wars Battlefront.

    One of the most thing I love in programming is that you imagine something that is easy in theory, but not that easy in reality, especially when trying to do something more realistic.

    Well, I am spending my last days studying the aim the bullets and found there is an easy and a not so easy way to do this stuff. I will begin with the “easy” part:


    1.1 – So, I guess most of fps that don’t have multiplayer or a 3rd person camera uses this simple way. For your consideration, I am using unity 5.3 and, for the hip-fire aiming point, added a Canvas, than the default image component, resizing it to a 3 by 3 scale, as teach in Merry Fragmas FPS - 2014 Unity tutorial.


    1.2 – Reading the forums and watching the videos, people say most of FPS don’t shot the bullets from the gun itself. They cast a ray exactly from the middle of screen:

    Ray ray = (Camera.main.ScreenPointToRay (new Vector3 (Screen.width * 0.5f, Screen.height * 0.5f, 0f)));


    1.3 – In this case the aim is “perfect” because the bullets will be instantiated always from the middle of screen to the middle of screen. So, there will not be some side effects seen on some of those games I listed before: In Battlefield 4, for instance, try to shoot something very close, even with aiming down sights and see that the bullet hits slightly under where your aiming is pointing at. For the third person example, in Star Wars Battlefront, depending on the position or angle you are shooting the ray of the gun collides first with a scene object and not hit the enemy you are aiming. I have both games and saw these issues in real gaming experience.


    1.4 - But this easy method has a “small” problem for, let`s say, a multiplayer game: instantiating the bullets from the middle of screen will result in unrealistic shots sometimes, for instance:




    1.5 - As you can see (and correct me if I am wrong), I could hit an enemy right above my face or ignore the big obstacle ahead my gun position and still hit something. I guess it applies for third person shooters and even first person when hip firing.

    2. - So I decided to fire “real” projectiles from the “real” gun position, using first a third person camera. Most tutorials I watched used raycasts to from the gun exit position to a future hit point. Subtracting this future hit point by my actual bullet exit position, I can find the direction my bullet has to go to hit what I am aiming:


    if (Physics.Raycast (ray, out hit, 5000f))

    {

    Vector3 dir = (hit.point - bulletExit.transform.position);

    transform.LookAt (hit.point); // I am using an empty game object in front of my gun to not rotate the gun itself

    GameObject instance = Instantiate (bulletModel, bulletExit.position, transform.rotation) as GameObject;

    instance.GetComponent<Rigidbody> ().velocity = transform.forward * bulletspeed; //

    }

    2.1 - This works reasonable well for the future contact point, except for some side effects, the same I noticed playing Star Wars Battlefront in 3rd person: depending on the angle you are shooting the bullets will hit first and object in front of the gun, even aiming away from this object. (The yellow arrow shows my pink aim dot (the image was cropped and resized, so don’t worry, I guarantee you it`s in the middle of screen) and the black arrow where the bullet hit.






    2.2 – And do not know if it`s the price to pay for the bullets being instantiated from the gun, but I am intended to pay for it, as even DICE could not solve this issue in Star Wars Battlefront.

    2.3 – The main problem I am facing now is when there is no hit points in front of me. So I don’t have a Z axis point reference. I tried two possible solutions, but found no one very good, specially when aiming at a future point to hit longer distance targets (I am not using aiming down sights for now, which would make thing easier, I believe):

    2.3.1 - Tried to build invisible walls around the scene, so I would have always a contact point, even far away o looking at sky. But besides being a complicated solution, it does not give me accurate aiming when trying to hit a target that has no collider and is closer to me, for instance, a flag. As the future point is far far away, let`s say, 2000 meters (the invisible wall) and the aim is in the middle of the flag, the bullet goes above the flag as it needs a very long vector to hit the 2000 meter point.


    2.3.2 - Tried to cast a ray and get a point at some distance, using:

    Vector3. futurePoint = ray.GetPoint(20f);

    Well, it works when the flag is at 20 meters in front of me, but the same inaccurate bullet trajectory results occurs closer or further. So I am stuck. Looking for the debug.drawray I am casting in the middle of my camera, I realize that the trajectory I want is there. But, for this, I had to instantiate the bullets from the middle of screen and return to the beginning again.


    I am concerned about shooting at the future point, trying to hit the enemy when he is running, like in Battlefield 4. Give some realistic physics to my game.

    Is there any workaround to these issues? I read and watched lots of tutorials but cannot find a solution. I need some answer or at least some direction... direction... eheh direction is what I need. Thanks.
     
    MetaDOS likes this.