Combining the 3rd person shooter aim script, with the fps aim script

Hey peeps. So, my game has come a long way. Made several new scripts for custom this and that:

Player movement:
1.Jesse Anders pointed me in the right direction for actually moving anything.
2.legend411 showed me to use forces, rather than actual movement. (much smoother thanks)
3.Vicenti who helped with everything related to looking.

Everything else I did. but as you can guess by this thread, im stuck on something.

so i have this tidbit of a problem. I decided to use the machinegun.js script from the fps tutorial, and it works great.
problem is, it aims at the middle of the screen. I need that thing to aim where the mouse is aiming.

I can see in the 3rd person shooter that the “player” aims where the mouse is aiming by creating an object that you cant see that moves with the mouse and changes how close it is by colliding with something, and aiming the players gun at that point.

that seems like 100% of what i need.

Ive deduced that the machinegun.js file (located here var range = 100.0;var fireRate = 0.005;var force = 10.0;var damage = 5.0;var - Pastebin.com)
has this function here that needs to be changed:

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);
		
	}

Part of the aiming for the 3rd person is done (i think) with ShooterGameCamera.cs (which btw i dont know c#) (found here using UnityEngine;using System.Collections;// 3rd person game-like camera con - Pastebin.com)

and

With gunaimer.cs (here using UnityEngine;using System.Collections;public class GunAimer : ExecutionO - Pastebin.com) which depends on pivot which ill need, the target, which is that sphere that moves around and the gun, of course where the bullets n stuff are coming from.

i deleted all other scripts and only broke it when i started deleting parts of the human, not sure what its depending on. if i replace the human with a sphere when the game is running, it works, but when i turn it off, delete the human, and do everything i did to make it work with just a sphere, it dosent work. odd stuff.

so anyway, i could use some help with this. after i get this done, im pretty much done (besides graphics)

basically what needs to happen:
PlayerWeapons <— This needs to point to the mouse target
MachineGun <--- child of weapons, because there will be more weapons the parent needs to move. ``machineGun <--- this is the gun AnyOtherGun ← because of the addition of guns the parent needs to move
``anyotherGun <— blah.

(oh and i prefer java, but if youre writing the code and im just copy pasting it to see if it works, then by all means use whatever u like to code in)

edit: btw, those three names up top that helped me, you can bet they will be in the credits of the game. and if this game actually makes money on a regular basis, you can also bet ill be sharing that money with them.

You will need to raycast the plane of the player in order to get a point of reference to shoot at.

I altered the code a bit just to get it to run… :wink:

function FireOneShot () {
	var hit : RaycastHit;
	
	var playerPlane = new Plane(Vector3.up, transform.position);
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hitdist = 0.0;
		
	if (playerPlane.Raycast (ray, hitdist)) {
		var direction = ray.GetPoint(hitdist)-transform.position;
		// Did we hit anything?
		if (Physics.Raycast (transform.position, direction, hit, 50.0)) {
			// Apply a force to the rigidbody we hit
			if (hit.rigidbody)
				hit.transform.root.rigidbody.AddForceAtPosition(5.0 * 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);
		}
	}
}

ill check this in a sec… looks way too easy lol

Edit: Hmm well you commented out what needed to be in there, i copy pasted this into the script and it dosent work.

basically what needs to happen:
PlayerWeapons <— This needs to point to the mouse target
MachineGun <--- child of weapons, because there will be more weapons the parent needs to move. ``machineGun <--- this is the gun AnyOtherGun ← because of the addition of guns the parent needs to move
``anyotherGun <— blah.

I’m not sure how others do it, but what I do is:

I have an orbit camera on my third person player that the player copies the transform of, so that when the camera rotates, the player rotates (just like the third person shooter example).

I want all bullets that the player fires to go toward the point in space that is directly in the center of the camera’s view, i have an object called “AimTarget”, which is just an empty transform, and I have a script on my camera that in Update(), shoots a ray from the center forward with a set range, and sets the AimObject’s transform.position to be the position of the ray’s endpoint.

Then, I have all projectiles that the player fires LookAt() the AimTarget’s position. In your case, where you’re using a ray and not a physical projectile, you’ll have to get the direction of the AimTarget by subtracting the machine gun’s barrel position and the AImTarget’s position, and shoot the ray in that direction.

I sortof understand that I think. Not sure how to work it though. What youre describing is pretty much exactly whats going on in the 3rd person shooter.

In my case, the camera and the player are the same object like in the First person shooter game. Which, by default also shoots at the center of the screen.

If i have an object that sits on my mouse at a certain distance (which i have a script for amazingly) if what im aiming at is really far, or really close, im not sure that it will be accurate.

What im going to shoot at will change position… so basically if you set that aim target in the 3rd person shooter tut to visible, youll see pretty much exactly what im needing. I would copy and paste that code and have the gun LookAt() but I cant seem to make that aim script work outside the 3rd person tut, and with a target that moves not with the center of the screen, but with the mouse.

Oh im just confused. I know what needs to be done, but doing it Im not sure.

Raycast from the center of the screen, aim the ray with the mouse?
Raycast from the gun, put an object wherever the ray hits move that object with the mouse, aim the gun to that object?
Raycast from the gun and aim that ray and the transform with the mouse position?

blah.

Lol…

bump