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

InverseTransformDirection

Discussion in 'Scripting' started by jlpeyton, Jan 15, 2020.

  1. jlpeyton

    jlpeyton

    Joined:
    Dec 22, 2017
    Posts:
    57
    I've looked at the Unity documentation but I don't understand what the InverseTransformDirection function does. When normalized, a vector keeps the same direction but its length is 1.0. In the function below, the input variable is equal to the transformed direction from world space to local space, being passed the direction normalized. What does this mean?

    Code (CSharp):
    1. public virtual void MoveToPosition(Vector3 targetPosition)
    2.         {
    3.             Vector3 dir = targetPosition - transform.position;
    4.             dir.y = 0;
    5.             //moveDirection = dir.normalized;
    6.             input = transform.InverseTransformDirection(dir.normalized);
    7.             // calculate input smooth
    8.             inputSmooth = Vector3.Lerp(inputSmooth, input, (isStrafing ? strafeSpeed.movementSmooth : freeSpeed.movementSmooth) * Time.deltaTime);
    9.         }
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Are you looking at someone else's code and trying to understand what it means? In that case, transforming the direction from world space to local space means that the vector will be relative to the transform, not relative to the world. For example, imagine you are in bed. Up in world space means the direction towards the sky (or against gravity, if you prefer). Up in your local space is the direction out of the top of your head, probably towards a wall. This allows you to do things like specify that you character should move to their right, not just move to the east (a world-space direction).
     
  3. jlpeyton

    jlpeyton

    Joined:
    Dec 22, 2017
    Posts:
    57
    Yeah I'm trying to breakdown code line by line and really try and understand not just what it does but why. Thank you for answering the why on this. That was an explanation worthy of an instructor.