Help with trying to understand transform.TransformDirection method

Hi there,

I’m following a beginners tutorial about raycasts at the moment which consists of a turret that the player is able to rotate left and right on the z-axis and fire a raycast from the top of the turret towards three possible targets. When the raycast has hit one of the targets it outputs a message in the console stating which target was hit.
Here is a video of what the end result of the tutorial looks like:

And here is the code shown below here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
    private float _moveInput;
    public LayerMask target;
    // Update is called once per frame
    void Update()
        _moveInput = Input.GetAxis("Horizontal");
        transform.Rotate(0, 0, -_moveInput);
        if (Input.GetKeyDown(KeyCode.Space))
            RaycastHit2D hit = Physics2D.Raycast(transform.position,transform.TransformDirection(Vector2.up),10f,target);
            Debug.DrawRay(transform.position, transform.TransformDirection(Vector2.up) * 10f,;
            if (hit)
                Debug.Log( + " has been hit with a raycast!");

As you can see in the above code the tutor uses a thing called “Transform.TransformDirection” which I don’t understand fully. My basic understanding of this function is that it takes the co-ordinate values that is stored within a vector and translates them to world space?
If I was to replace the second parameter in the Physics2D.Raycast method in the if statement with just “Vector2.up” the raycast just fires straight up regardless of the z-value of the “local rotation” of the player game object. I don’t understand why this is? It seems that the Transform.TransformDirection function makes sure the origin of the raycast will start at the top of the turret in the direction that it is pointing towards.

I’ve been looking at videos on Youtube about the difference between local and world space but it still doesn’t help me understand the above example I have described.

Any helpful information about my question would be appreciated :slight_smile:

So local space means relative to a specific object while world space means what Unity sees.
Imagine you are making a game about astronauts floating around in space. You want an astronaut to shoot a laser up over his head, but how do you actually do that? You need to know the direction to shoot the laser in, and that direction relative to the astronaut is just “up”, or in Vector terms that direction is (0, 1, 0). But if this astronaut is floating around in space and rotating every which way, you need to know what the ACTUAL direction is relative to the X, Y, and Z axis in the Unity engine. If the astronaut’s head is pointing along the X axis, then “up” for the astronaut is actually “right” according to Unity world coordinate system.
Transform.TransformDirection basically is just a function where you say "OK, I know the direction I want relative to my astronaut, can you please tell me what the direction is relative to the Unity Coord System so that I can do something useful with it. So if you astronaut’s head is pointing along the Unity X axis, you can say transform.TransformDirection(Vector3.Up) and Unity will tell you “Oh, the actual direction for this is along the X axis”.
Transform.Transform direction is more useful for more complicated relative vectors, since for the basic directions (up, forward, right) you can just call transform.up, transform.forward, and transform.right.