Search Unity

Dont really get this concept, need help visualising it.

Discussion in 'Scripting' started by BubyMB, Jul 17, 2016.

  1. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Hey guys, I am making a moba style game which obviously has battling in it. So I am first making a simple shooting script. that shoots a bullet with 3 attributes, the team, amount of damage when hit, and speed. I have the shooting all set but how would I make it damage the item it hit, would I put a hit detector script on what CAN be hit or put a hit detector script on the actual bullet? Same goes for melee how could i do that? do I have to make the sword or item a seperate item or can i make it all with the player
     
    Last edited: Jul 17, 2016
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    I would set up a collider on the bullet and implement OnCollisionEnter or OnTriggerEnter in the bullet script(which I assume you have). You can then check if the object that the bullet collides with is an enemy and handle damage. The same thing applies to your sword.
     
  3. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,572
    This will run into the infamous "bullets not registering collisions!" problem if the bullets travel at a high enough speed(and the physics step is not increased). You'll likely need to do some ray-casting towards the direction(probably forward) vector of the bullet at a length of the bullet's velocity+some epsilon. Use this for collision detection instead of relying on the object not traveling through the collider in between physic steps.

    However, if your bullets are moving slow enough the above solution is plenty.
     
    jimroberts likes this.
  4. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    This really depends on the unity version... as of 5+ collisions are pretty darn solid... there was a thread a while ago where this came up and I ran a quick test with a Web player... pure physics based gun with projectiles set to 1000 m/'s or so.(from what I remember). After I post this I'll look up the thread and edit this with the link.

    EDIT:http://forum.unity3d.com/threads/hi...vs-projectile-simulation.334463/#post-2188153
     
    landon912 and jimroberts like this.
  5. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    I think this is a result of the continuous detection mode which is the engines implementation of what landon91235 suggested. Thanks for adding an important detail that both of us seem to have forgotten about. :D
     
    novashot and landon912 like this.
unityunity