- Home /
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
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
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?
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
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 :)
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;
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?
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.