Shooting not lining up.

General Discussion on the Unity Engine.
Post Reply
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Shooting not lining up.

Post by alexrules01 »

Well i've completed the Unity FPS tutorial, which was rewarding but VERY fustrating!

I am having trouble where a bullet from a machine gun collides with another object, whilst looking through the crosshair, the impact isn't in the centre of the crosshair.
I have looked at the complete solution and they line it up perfectly. (Also, they use a GUITexture for the crosshair, where the tutorial makes you use a script to draw the crosshair.)

Has anyone else done this tutorial? If so, did it all work for you??

If not, does anyone have any ideas?

This is the code for the MachineGun, I can't think the error would be anywhere else, unless a game object needs to positioned differently.

Code: Select all

var range = 100.0;
var fireRate = 0.05;
var force = 10.0;
var damage = 5.0;
var bulletsPerClip = 40;
var clips = 20;
var reloadTime = 0.5;
private var hitParticles : ParticleEmitter;
var muzzleFlash : Renderer;

private var bulletsLeft : int = 0;
private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;

function Start () {
	hitParticles = GetComponentInChildren(ParticleEmitter);
	
	// We don't want to emit particles all the time, only when we hit something.
	if (hitParticles)
		hitParticles.emit = false;
	bulletsLeft = bulletsPerClip;
}

function LateUpdate() {
	if (muzzleFlash) {
		// We shot this frame, enable the muzzle flash
		if (m_LastFrameShot == Time.frameCount) {
			muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
			muzzleFlash.enabled = true;

			if (audio) {
				if (!audio.isPlaying)
					audio.Play();
				audio.loop = true;
			}
		} else {
		// We didn't, disable the muzzle flash
			muzzleFlash.enabled = false;
			enabled = false;
			
			// Play sound
			if (audio)
			{
				audio.loop = false;
			}
		}
	}
}

function Fire () {
	if (bulletsLeft == 0)
		return;
	
	// If there is more than one bullet between the last and this frame
	// Reset the nextFireTime
	if (Time.time - fireRate > nextFireTime)
		nextFireTime = Time.time - Time.deltaTime;
	
	// Keep firing until we used up the fire time
	while( nextFireTime < Time.time && bulletsLeft != 0) {
		FireOneShot();
		nextFireTime += fireRate;
	}
}

function FireOneShot () {
	var direction = transform.TransformDirection(Vector3.forward);
	var hit : RaycastHit;
	
	// Did we hit anything?
	if (Physics.Raycast (transform.position, direction, hit, range)) {
		// Apply a force to the rigidbody we hit
		if (hit.rigidbody)
			hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
		
		// Place the particle system for spawing out of place where we hit the surface!
		// And spawn a couple of particles
		if (hitParticles) {
			hitParticles.transform.position = hit.point;
			hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
			hitParticles.Emit();
		}

		// Send a damage message to the hit object			
		hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
	}
	
	bulletsLeft--;

	// Register that we shot this frame,
	// so that the LateUpdate function enabled the muzzleflash renderer for one frame
	m_LastFrameShot = Time.frameCount;
	enabled = true;
	
	// Reload gun in reload Time		
	if (bulletsLeft == 0)
		Reload();			
}

function Reload () {

	// Wait for reload time first - then add more bullets!
	yield WaitForSeconds(reloadTime);

	// We have a clip left reload
	if (clips > 0) {
		clips--;
		bulletsLeft = bulletsPerClip;
	}
}

function GetBulletsLeft () {
	return bulletsLeft;
}
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Shooting not lining up.

Post by SpiritWebb »

I have done this tutorial several times. What is the error?
Image

Image
User avatar
Callan S.
Posts: 2043
Joined: Sun Jan 24, 2010 5:43 am

Re: Shooting not lining up.

Post by Callan S. »

Alex, does the bullet always end up off the center of the cross hairs in the same way? Eg, always to the top left, or something like that? Checking whether it's a consistant drift?
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Shooting not lining up.

Post by alexrules01 »

Callan S. wrote:Alex, does the bullet always end up off the center of the cross hairs in the same way? Eg, always to the top left, or something like that? Checking whether it's a consistant drift?
Its always to the bottom right, and yes it consistantly hits there, so Im guessing theres a positioning error somewhere

Here are 3 screens from close, medium and long range shooting at the same spot. Its kinda hard to tell where its exactly hitting, but you can tell it isnt hitting dead on.

Short Range
http://imageshack.us/photo/my-images/33/shortn.png/

Medium range
http://imageshack.us/photo/my-images/29/medc.png/

Long range
http://imageshack.us/photo/my-images/801/longz.png/
SpiritWebb wrote:I have done this tutorial several times. What is the error?
Luckily no major error, just the positioning lol :( Several times, must be pro at it now lol. Just one question, if you look at the 3rd picture, and the GUI at the bottom right hand corner, the rockets don't go all the way to the end of the box, know whats going on thier?
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Shooting not lining up.

Post by SpiritWebb »

I had the same issue with the "not hitting straight on" but, I believe it is, as its the particle emitter makes it appear its not when it strikes. With the GUI, it looks like it might be a little stretched, try bringing it down a little in terms of width!
Image

Image
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Shooting not lining up.

Post by alexrules01 »

SpiritWebb wrote:I had the same issue with the "not hitting straight on" but, I believe it is, as its the particle emitter makes it appear its not when it strikes. With the GUI, it looks like it might be a little stretched, try bringing it down a little in terms of width!
I turned off maximize on play on the game screen, so the width is shrunken, but it does the exact same thing. Was the stretched GUI your problem?
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Shooting not lining up.

Post by SpiritWebb »

No, but then again I never gave it much thought. It has been a while since I have done that tutorial
Image

Image
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Shooting not lining up.

Post by alexrules01 »

SpiritWebb wrote:No, but then again I never gave it much thought. It has been a while since I have done that tutorial
Fair enough. its no BIG problem, but it would be cool to get it sorted so I don't have the problem in the future.
I was also watching a video of someone using probably the same or a very similar set up and reading the comments, and I believe the uploader said the raycast was the problem, I will try and find out.

Its weird how the complete game got it working 100% and accurate though :S
User avatar
Callan S.
Posts: 2043
Joined: Sun Jan 24, 2010 5:43 am

Re: Shooting not lining up.

Post by Callan S. »

I'd almost suspect the raycast or the particle emitter uses some sort of box to do it's thing, and it's taking it's collision point from the bottom right corner of the box, instead of the center.
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Shooting not lining up.

Post by alexrules01 »

Callan S. wrote:I'd almost suspect the raycast or the particle emitter uses some sort of box to do it's thing, and it's taking it's collision point from the bottom right corner of the box, instead of the center.
Ok thanks mate, i will have a look at it and try and figure how to fix it lol.
Post Reply

Return to “General Unity”