Help with weapon purchase system (no menu,GTA style)

edit:

nobody? I am trying to figure out the pickup system in the weaponmanager script, so that I can incorporate purchase system. The idea is, to get rid of “pickups”, and just collide with weapon, if have enough money, buy it if its not equipt, if it is, you buy the ammo instead. So no special ammo pickups for each gun as there is now. The things is, I am trying to learn how the heck it is already set up, so that I know how to change it. I’m new to scripting, so figuring this out is a headache, would appreciate some insight as to what is going on, how is it happening, what is the code doing.

Thanks

The whole reason I skipped this post is that it was way too much code for a simple question. You are actually asking someone to go in and fix all of your code.

As it seems, you need to step back and figure out how you want your code to work. In GTA IV, you simply looked at a weapon and pressed a button to buy it. You can accomplish this using a few different methods.

  1. attach a trigger to it, that allows you to buy the weapon when you are inside of it.
  2. attach a trigger to your main character that manages all incoming objects with a purchasable script attached.
  3. gather all purchasable weapons on this map.

in 2 and 3, you can then use the direction to the weapon, distance and Dot to get the closest weapon the player is facing then let that script show it’s gui to purchase.

If you are then running a GTA camera, then you basically always look where you are going, and you have no mouse. If you are running a WoW style camera, that is not the case.

GTA just means you press F or whatever like your entering a vehicle to make your purchase.

If you read my posts, you will see I already decided on a OnTrigger collider system(see logic/scriptish thing in 1st code box), as you have suggested. I am not asking for anyone to fix the code, or give any bit of code, just help in understanding what is going on in the weaponmanager script. If I can learn how it is working already, I’m sure I can figure out how to change it myself, eventually :slight_smile:

heh, yeah, this thing needs to be compartmentalized.

The WeaponsManager is using a raycast setup, not an OnTrigger setup.

As far as I can tell, the manager has all the weapons in teh game already. All you do is select the ones that you want to use at the time. So there is no “buy” a weapon. You really just say. What do I have ammo for, and can I use it.

I would honestly rewrite all of these scripts so that you can make maximum use of them and learn exactly how they work (or should work) for your situation.

This is one of the reasons I always say “build your own code”. If you dont know what it does, and dont know how it works, how are you going to make it work.

Let me give you some builder code. This is the basics of a simple “look at” pick up system. It uses a collider that is not part of the gun mechanics and does not affect physics. I commented it so that you can see the basics of how it works.

#pragma strict
// place this object on a trigger
// trigger

var cost : int = 200;
var keyPosition : int = 1;
var gun : Transform;
var player : Transform;

function Start () {
	// the collider is only there to see if we are pointing at it
	// it is a trigger adn all we have to do is run a Raycast against it.
	// as a trigger it never affects physics.
	// we could also try just disabling it. That will make it not affect anything.
	// see if we can run a Raycast against a disabled collider???
	if(!collider) gameObject.AddComponent(BoxCollider);
	collider.isTrigger = true;
	collider.enabled = false;
	
	player = GameObject.FindGameObjectWithTag("Player").transform;
}

function Update () {
	// no player, no weapons
	if(!player) return;
	// if the root of the weapon is the player, then the player is holding it. Dont do anything
	if(transform.root == player) return;
	// if someone else is holding it, we can't pick it up.
	if(transform.root != gun) return;
	// if we are close enough to it, but not holding it.
	if((player.position - transform.position).magnitude < 5){
		// if we press the E key to pick it up.
		if(Input.GetKeyDown(KeyCode.E)){
			// create a ray, and see if our ray intersects with the collider
			var ray : Ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0));
			var hit : RaycastHit;
			if(collider.Raycast(ray, hit, Mathf.Infinity)){
				// if we have a cost, then we have to buy it.
				if(cost > 0){
					// Purchase a weapon
					PurchaseWeapon();
				} else {
					// Pick up a weapon
					PickupWeapon(true);
				}
			}
		}
	}
}

function PurchaseWeapon(){
	// subtract Credits
	PickupWeapon(false);
}

function PickupWeapon(RemoveWeapon : boolean){
	// add weapon to inventory if needed
	// remove the weapon if not needed
	// remember to set the cost of any weapon we equip to zero!!!
	// so we dont buy it when picking it back up.
	// add ammo to inventory
}

This type of script works well with another gun script I created:

http://forum.unity3d.com/threads/139918-shooting-bullets(seeing-when-there-are-no-bullets)?p=953293&viewfull=1#post953293

Then its just a matter of filling in some of the blanks and creating your own WeaponsManager. Simpler the better.

You can modify this script to be on Ammo, by omitting the parts where you add the weapon. I would do ammo on rollover. Much simpler, but if your after the whole Borderlands, you could easily adapt it to that style.

Ok, yeah you have a good point. I think I will just write it from scratch. I will need to learn unityscript much better first though. I have trouble finding good sources, since unityscript is different than java, and the resources I do find don’t really tell you how it’'s useful from a gaming aspect. Any suggestions?Thanks for the code samples, I’m sure they will come in handy. Don’t know what you mean by rollover, never played borderlands either(call me a weirdo).

heh, borderlands pickup system is wher e you point at something, press a button to pick it up. Much like the gta thing you are talking about. In GTA, if you drop a weapon, its gone, but in Borderlands and most shoot’em ups you can pick the weapon back up.

UnityScripts syntax is a lot like Actionscript 3, not Javascript. It was not the best idea to call it Javascript. If you are just learning, I suggest C#, not Javascript. It is incredibly well documented and not far from the same syntax. I have the benefit of knowing many languages, so all they are is syntax to me now. Your best bet for anything Unity is the documentation. It contains JS, C# and Boo reprensetations of 99% of the code they offer.

Start Simple, then as you get the syntax and logic down, the rest is easier.

Skip up to about 2:55 or so, and you will see things drop off the killed creature and he quickly scans over them and picks up what he wants.

Wow, that game actually looks pretty cool. I hear good things about it too. Wonder if it was made from the same studio or artists as the Walking Dead game, because they are both 3D comic looking. Time to start a long road of learning unityscript lol. Sucks because my game is basically fully made besides the scripting. But I’ve come this far!

Edit: Also, I’m not going for a pickup system like that though(loot), just a purchase system. Forgot you were making a point with that video lol