- Home /
Multiple Melee Weapons
Haha, I could barely log into this place!
Ok so I'm attempting to make an RPG-like game. I want to have multiple weapons, inventory, stuff like that, but so far I'm only on the weapon part. I've been able to get a spear working with animations an a heirarchy like this: FPS Player-> Main Camera-> Weapon Holder-> Weapon Mesh
I've animated the Weapon Holder, and that's worked good, but now I need to learn how to switch weapons and I don't know what to do next. I was going to try to destroy the Weapon Mesh when I press a button, and then instantiate it on the weapon holder(I've made a weapon template in Blender so all the meshes are zeroed in on the same spot to preserve the animations) but I've had trouble with instantiating in C# and can't quite wrap my head around it.
I have my script on the Weapon holder, but need to destroy the child object and then instantiate a weapon prefab. Also my script wouldn't be helpful, all I have is GetButton functions for the animations.
I want to learn this stuff so if you could post an example, and explain it to me it would be really appreciated.
On a side note, for the combat I was thinking of using trigger collisions, but it damages enemies even if you walk into them. If you could also help me understand raycasting a bit for that then it'd be helpful, if not it would just be a matter of instantiating an object with a box collider when i hit the attack button and destroying it after.
Answer by aldonaletto · Jul 01, 2011 at 09:20 PM
This script creates an array called weapons in the Inspector. Set the array size and drag your weapon prefabs to each empty slot. Create an empty object at the weapon position, assign this script to it and child it to Weapon Holder.
var weapons: GameObject[];
private var curWeapon: GameObject;
function ChangeWeapon(weapon:int){
if (curWeapon){
Destroy(curWeapon);
}
curWeapon = Instantiate(weapons[curWeapon],transform.position,transform.rotation);
}
function Update(){
if (Input.GetKeyDown("1")) ChangeWeapon(0);
if (Input.GetKeyDown("2")) ChangeWeapon(1);
if (Input.GetKeyDown("3")) ChangeWeapon(2);
}
You must adjust position and rotation of the empty object because the weapons will copy these characteristics when instantiated. Any time you call ChangeWeapon, the current weapon instance will be destroyed and the new one will be instantiated in its place.
About the attacks: you can add a trigger and a different tag to each weapon prefab, and in the enemies script use something like this:
function OnTriggerEnter(c:Collider){
if (c.tag == "axe"){
ApplyDamage(10);
}
if (c.tag == "hammer"){
ApplyDamage(8);
}
...
}
Of course, ApplyDamage is a function you must create, which will reduce enemy health and do whatever else you want when the enemy is hit (splash blood, scream etc.). I've not tested these scripts, so if you have some trouble feel free to ask for help here! (in the comments, please)
Answer by xanderdood · Dec 11, 2011 at 06:22 PM
InvalidCastException: Cannot cast from source type to destination type.
ive discovered a problem with this code! not sure why!
Your answer
Follow this Question
Related Questions
Melee attack comblat 1 Answer
Hitting Multiple Enemies 1 Answer
Melee Combat 1 Answer