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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Help in rotate vector3.forward oriented positions towards player transform.forward

Discussion in 'Scripting' started by leandrotenebrae, May 5, 2023.

  1. leandrotenebrae

    leandrotenebrae

    Joined:
    Mar 19, 2020
    Posts:
    3
    I'm currently working on a system that shows the range of an attack inside a combat grid. This range works pretty simple: it is a scriptable object which has a list of vector3 positions representing each position direction related to the forward of the character. The problem is they are being stored in a Vector3.forward orientation and when executed I have to convert them to the transform.forward of the player, which vary. I know how rotations work but somehow I'm not quite understanding how to solve it. Any help?

    Those yellow squares are the range positions showing in a global forward direction when the player is looking in an other direction



    This is the scriptable range positions intented to form a "T" as shown in the image above


    The code, without rotation or translation
    Code (CSharp):
    1.  
    2. foreach (var position in rangeArea.positions)
    3.         {
    4.             Vector3 worldPosition = player.localPosition + position * Grid.GetCellSize();
    5.  
    6.             Collider [] colliders = Physics.OverlapBox(worldPosition, new Vector3(2f, 2, 2f));
    7.  
    8.             if (colliders.Length > 0)
    9.             {
    10.                 foreach (var collision in colliders)
    11.                 {
    12.                     if (collision.CompareTag("Combat Position"))
    13.                     {
    14.                         collision.GetComponent<CombatPositionStateController>()?.SetToRangeState();
    15.                     }
    16.                 }
    17.             }
    18.         }
    19.  
     

    Attached Files:

    Last edited: May 5, 2023
  2. leandrotenebrae

    leandrotenebrae

    Joined:
    Mar 19, 2020
    Posts:
    3
    This code worked for me

    Code (CSharp):
    1.  
    2. Vector3 targetDirection = player.TransformDirection(Vector3.forward);
    3. Quaternion correctedRotation = Quaternion.FromToRotation(Vector3.forward, targetDirection);
    4. Vector3 localDirection = correctedRotation * position;
    5. Vector3 worldPosition = player.localPosition + localDirection * Grid.GetCellSize();
    6.