Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Tageos · Jul 19, 2015 at 07:39 PM · c#colliderattacking

Best way to attack

Hello again :)

What would you say is the best way to do a meele-attack?

When i press LMB i swing my weapon. I am thinking of just instantiating a collider somewhere along the swing-sprite for as long as the swing-sprite is in the universe. The swing-sprite collider detects enemy-colliders and damages the enemy that touches this collider. Are there any other way of doing this that is better? Performance-wise and gameplay-wise.

Thank you.

/Taegos

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
1
Best Answer

Answer by Zeronev · Jul 20, 2015 at 02:55 AM

Please dont instantiate colliders for melee attacking or so, that can turn complicated later on. Use raycasting instead, its more useful, if the player is in range of the raycast, you can remove health of the enemy (attacking)

Check http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Comment
Add comment · Show 4 · 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 Tageos · Jul 20, 2015 at 03:41 PM 0
Share

okay, thank you. So for meele attacks i use raycasting, but for range attacks like shooting it is better to instantiate a bullet-gameobject with a collider?

avatar image Zeronev · Jul 23, 2015 at 04:24 PM 0
Share

You can use both, raycasting for shooting the object, and using a mesh collider or box collider for hitting the enemy, that way only the bullet can damage the enemy and not the ray itself

avatar image Tageos · Jul 27, 2015 at 01:09 PM 0
Share

okay thanks :)

avatar image Zeronev · Jul 27, 2015 at 11:49 PM 0
Share

Yeah, remember to instantiate the bullet by attaching an empty game object to the barrel end of whatever gun you want, and in that empty game object you can add the script to instantiate the bullet in the desired direction :)

avatar image
1

Answer by mconradie · Jul 19, 2015 at 07:59 PM

I think that should work well but don't instantiate a new one every time that you swing your weapon, just enable at the beginning of the swing and then disable after the swing.

 this.GetComponent<WhateverCollider>().enabled = false;
Comment
Add comment · Show 2 · 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 Tageos · Jul 19, 2015 at 08:24 PM 0
Share

But if i want to instantiate a circle collider during the swing but alredy have a circle collider attached to the player-prefab that i dont want to fiddle with?

Wont

this.GetComponent().enabled = false;

disable all the colliders of the same type that is attached to the player?

avatar image lumbryga · Mar 26, 2019 at 08:09 PM 0
Share

I use this method mentioned by mconradie.

It seems to me that putting your triggers in child gameObjects of your objects is a good practice. Then, you put the script with that trigger logic in the same child gameObject as the trigger collider, and you can reference your base gameObject scripts via GetComponentInParent() and reference your triggers in the base scripts via GetComponentInChild. This way you have no problem with multiple triggers/colliders in one gameObject, and can enable/disable any of them as you like.

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

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to detect which side of a box collider was hit? (Top, Bottom, Right, Left) 2D game/ C# 3 Answers

Checking gameobject in Collider range only on method call 1 Answer

Raycast isn't working as expected 1 Answer


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