Search Unity

Spawn Bullet problem

Discussion in 'Scripting' started by Kovenant, Oct 1, 2014.

  1. Kovenant

    Kovenant

    Joined:
    Sep 18, 2013
    Posts:
    254
    I'm having trouble spawning bullets at the right position in my game.. The selected Game Object in the Scene is the bullet spawn point.. and the screenshot from in-game is where the bullets end up when I press Fire (L Mousebutton).. No matter how I position the bullet_spawn in the scene it always ends up in the wrong place in-game (not at the end of the gun barrell).. Right now I'm only Instantiating bullet without adding any force to it just to trail and error the bullet spawn point. I don't know if it has to do with that I have a Weapon Camera and a Eye/Head cam.. Because when I'm looking at the Scene View as I play it looks like the bullets spawn where they should, but they look so off in game..

    bullet_spawn.jpg

    Here's my code for instantiating the bullet:
    Code (JavaScript):
    1.     // Shoot bullet
    2.     if(Input.GetButton("Fire1"))
    3.     {
    4.         if(waitTillNextFire <= 0)
    5.         {
    6.             if(bullet)
    7.                 Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    8.             waitTillNextFire = 1;
    9.         }
    10.     }
    11.    
    12.     waitTillNextFire -= Time.deltaTime * fireRate;[/code
     
  2. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    where is the center of mass for bullet?
    are you watching the scene view in real-time to see that they're spawning on bulletSpawn?
    is there any parenting of objects in the scene editor?
    if so is there any scaling or rotating being done through the chain?
     
  3. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    Well in this chase, similar behavior might happen if:
    - both cameras are enabled at the same time
    - only one of the renders bullets
    - and render mode is depth only.

    this way the camera that renders the bullets would render it over the one that doesn't and since the view might be different it might look like offset.

    try disabling one of the cameras to see if that's the cause.

    Oh and look in to Object pooling, you should not use instantiate to fire bullets it will just annoy the garbage collector or cause memory leak.
     
    Kovenant likes this.
  4. Kovenant

    Kovenant

    Joined:
    Sep 18, 2013
    Posts:
    254
    Thank you.. I will try that... Once I get this right I will actually not instantiate bullets anymore, but use Raycasting instead.. I just use a cube-bullet to visually trail and error this.. :)
     
  5. Kovenant

    Kovenant

    Joined:
    Sep 18, 2013
    Posts:
    254
    Yeah.. that actually made the bullets show up in the right place in-game.. so... what should I do now to avoid the weapon from going through objects since I can't use a weapon cam with depth only? :)
     
  6. Kovenant

    Kovenant

    Joined:
    Sep 18, 2013
    Posts:
    254
    Nevermind... I can have the weapon cam.. I just need to have the same mouse sensitivity on the weapon cam as on the eye cam... Thank you all for the help...

    I was on the edge of shooting myself in the head with this, but then I realised; the bullet would probably miss.... ;-)
     
  7. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    If you want aim down sights you just disable the weapon camera component (but leave it as handy debug view) then make script that allows you to toggle main camera to that position and settings. To do this you likely need a script that smootly changers the position rotation and settings of the camera towards the values specified in weaponview.

    You could use iTween for the position and rotation handling. It's a free script that contains a ton of useful tweens for game. You could even call iTween.PunchPosition to the camera when shot is fired to simulate the weapon kick.
     
    Kovenant likes this.
  8. Kovenant

    Kovenant

    Joined:
    Sep 18, 2013
    Posts:
    254
    I got it to work just as I intended it to work now..

    I have another problem, I'll stay on this thread because its somewhat bullet-related too...

    I'm trying to Instantiate bullet holes as I'm shooting at stuff that belong to the level using this code:
    Code (JavaScript):
    1. var maxDist : float = 10000;
    2. var decalHitWall : GameObject;
    3. var floatInFront : float = 0.001;
    4.  
    5. function Awake()
    6. {
    7.     var hit : RaycastHit;
    8.    
    9.     if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit, maxDist))
    10.     {
    11.         if(decalHitWall && hit.transform.tag == "Level Parts")
    12.         {
    13.             // THIS PART DOESN'T WORK JUST YET.. DECAL ROTATION PROBLEM
    14.             // The bullet hole doesn't lie flat on the wall, instead it has a strange rotation.
    15.             Instantiate(decalHitWall, hit.point + (hit.normal * floatInFront), Quaternion.LookRotation(hit.normal));
    16.         }
    17.     }
    18. }
    The problem is that the decal doesn't stay flat on the wall (or floor or whatever) I'm hitting... instead it has a strange rotation to it, I've tried to give the Prefab a rotation to fix this.. but it still shows up wrong in-game when I'm shooting at walls and what not.