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.. Here's my code for instantiating the bullet: Code (JavaScript): // Shoot bullet if(Input.GetButton("Fire1")) { if(waitTillNextFire <= 0) { if(bullet) Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation); waitTillNextFire = 1; } } waitTillNextFire -= Time.deltaTime * fireRate;[/code
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?
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.
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..
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?
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.... ;-)
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.
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): var maxDist : float = 10000; var decalHitWall : GameObject; var floatInFront : float = 0.001; function Awake() { var hit : RaycastHit; if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit, maxDist)) { if(decalHitWall && hit.transform.tag == "Level Parts") { // THIS PART DOESN'T WORK JUST YET.. DECAL ROTATION PROBLEM // The bullet hole doesn't lie flat on the wall, instead it has a strange rotation. Instantiate(decalHitWall, hit.point + (hit.normal * floatInFront), Quaternion.LookRotation(hit.normal)); } } } 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.