Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Looking for a way to switch focus targets in Unity

Discussion in 'Scripting' started by sedukafe, Aug 16, 2023.

  1. sedukafe

    sedukafe

    Joined:
    Aug 10, 2018
    Posts:
    4
    Hello everyone.

    I am currently working on a souls like project, and I'm struggling to find a way to switch focusing targets.

    The goal: I've a simple focusing on target system, and I'd like to be able, while focusing, to switch to the nearest target in the direction of the right stick.

    My idea: I was thinking doing this by translating every 3d target point to a 2d plane with matrices or any Unity method to then calculate the closest point in the stick direction, but I've no idea how I can do it, and there's maybe a easiest and more intuitive way to do it.

    I tried to look at various tutorials, but they aren't helping with this or aren't explaining well enough for me to understand what they are doing and why. Do anyone have any tips?

    Thank you very much!
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    You can just get the objects within range and then loop over them and check vector3.Distance
     
  3. sedukafe

    sedukafe

    Joined:
    Aug 10, 2018
    Posts:
    4
    I need the direction. My focus is already working, and it's already selecting the closest target from the forward view. The problem here, is that I have to deal with the input direction
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    Maybe create a bias using the angle between the direction and position? Dot product might be helpful for this
     
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Code (CSharp):
    1. Vector3 direction = (currentTargetPos - myPos).normalized;
    Something close to that ^^. Been awhile since I wrote them out manually.
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    Exactly. Then once you find the direction vector all you have to do is find the dot product of the direction vector and your player's transform.right. Then if the result is positive the enemy is on the right, if it's negative then the enemy is on the left.
     
  7. sedukafe

    sedukafe

    Joined:
    Aug 10, 2018
    Posts:
    4
    Thank you for your answers, I've done something a bit different, but you inspired me for this idea. The idea is to translate the input direction on the current target, to get the real direction (Using the Quaternion.FromToRotation method). Then, to check if the enemy is left or right from the input direction, I've to use the dot product. The thing is that something is still missing. I'd like to check if the target is within a certain angle.
    But I have an idea of how to do this:
    • Calculate the cross product of the translated Input direction and the "currentTargetPos - myPos" (both normalized of course).
    • Compare the dot product of the direction vector with a target and the dot product of the cross product with the target.
    • If the absolute value of the cross product is higher than the dot product, then it won't be considered for the final result (could also make it in a way that the detection angle could be changed, but for me it's okay)
    • Then I simply get the target with the lowest dot as the new target (And if there's no target, I take the furthest target in the opposite direction, so two different checks have to be made during every loop until a valid target is found in the stick direction)
     
    Last edited: Aug 18, 2023
    wideeyenow_unity and DevDunk like this.
  8. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    A capsule cast also might just work now I think of it