Search Unity

Lightsaber projectile deflect sytem

Discussion in 'General Discussion' started by Xwad, Oct 30, 2022.

  1. Xwad

    Xwad

    Joined:
    May 14, 2020
    Posts:
    41
    Hello!
    I am trying to make a lightsaber projectile deflect system. For the first step i want to detect all projectiles that are moving towards the player. What method do you recommend for that? I tried it by adding a sphere trigger collider around the player and check if any projectile hits the collider and then get the contact point and the projectiles rigidbody velocity. My problem is that the OnTriggerEnter detection is not contnuous so the contact point is not accurate when using faster projectiles. I also tried a sphere cast but it's also not accurate. Maybe i have to make a custom collision detection system?
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,139
    Cast from the projectile source, not a sphere around the player.
     
    Xwad and angrypenguin like this.
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    First up, you're in the wrong place. This is General Discussion, which is by definition "not a support forum". It sounds like you're after the Scripting area, which is exactly the place to ask about algorithms and what objects to use and how to script them. :)

    Why does the contact point matter?

    In my head, if I want to write some code to deflect projectiles which would hit an object, here's roughly how I'd do it:
    1. Track projectiles within a certain distance. As mentioned in your other post on this, I'd quite possibly use OnTriggerEnter to add them to a List. Your chosen distance has to be far enough that your fasted projectile can't reach from outside to hit the target object in a single physics step.
    2. In FixedUpdate(), iterate over that list and check which ones are going towards the player. I'd do this with a simple raycast along the projectile's velocity vector, with a length of the distance it will move in the next physics step. If that is going to get "close enough" to the object, then I'd call DeflectProjectile(...) to deal with it. Otherwise, just go to the next projectile.
    3. In DeflectProjectile(...) change it's velocity as desired. Generically, you can make the projectile go in any direction which is away from the player. You could use a physics reflection so it's as if it hit a mirror, or you could just redirect it along a normal pointing away from the player's origin. You mentioned light sabers, though, and I'll point out that in Star Wars games the projectiles are often directed towards a nearby enemy, or the person who fired them.
    4. If you have a list, don't forget to remove projectiles when you're done with them! Either when you deflect them, or in OnTriggerExit for stuff which misses. Otherwise weird stuff will happen.

    I can't think of anything in there which depends on the initial contact point with the trigger. I could be missing something. What are you thinking you need it for?

    - - -

    Edit: Even if you do need it for the rules of your game, it can be calculated with information available to you. You've got the current projectile position and its velocity, which define the line it is moving along. You also know the position and radius of your trigger sphere. You can look up a line-sphere collision formula and plug those values into it.

    That'll usually give you two answers, and you want one in the "backwards" direction of the projectile's velocity.
     
    Xwad likes this.
  4. Xwad

    Xwad

    Joined:
    May 14, 2020
    Posts:
    41
    Thanks for your help angrypenguin! I already solved it and did it exactly like you said by watched this video:


    My first attempt was to detect the projectile with a sphere collider around the player but i couldn't make it work that way.