Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Heya0091 · Jul 01, 2011 at 05:33 PM · collidermultiplemeleeweaponscombat

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Melee attack comblat 1 Answer

Hitting Multiple Enemies 1 Answer

Melee Combat 1 Answer

How can i detect collision in melee combat? 2 Answers

Sword Combat System: Detect if player in range 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges