Raycast only occurs on start

Hello,
so I have a little problem with raycasting. I want to put a certain object on raycast hit location, but it appears to work only once on startup and then stop. Even if I move the starting location of raycast or rotate it, the object that should move to raycast hit, won’t move.

Code:

using UnityEngine;

public class Crosshair : MonoBehaviour {

public GameObject crosshair;
private Vector3 hitLoc;

void Update ()
{
    RaycastHit hit;
    if (Physics.Raycast(transform.position, Vector3.forward, out hit))
    {
        hitLoc = hit.transform.position;
    }
    crosshair.transform.position = hitLoc;
}

}

Thank you for your time.

Not sure what you need, but do you need to rotate the vector forward by the transform rotation? Otherwise the ray always points only in the forard direction in world coords

@JavaMcGee thank you for your answer.

This is an example picture.
I found out raycast does indeed goes just forward in world space so I made a little calculation:

{
    Vector3 objectForward = transform.TransformDirection(Vector3.forward);
    Debug.DrawRay(transform.position, objectForward, Color.green);
    RaycastHit hit;
    if (Physics.Raycast(transform.position, objectForward, out hit))
    {
        hitLoc = hit.transform.position;
    }
    crosshair.transform.position = hitLoc;
}

In debug, everything is okey, raycast is indeed going from end of the gun forward.
But now, my “aim” object only spawns on the end at every 90 degrees, it’s not really following raycast. Or to be precise, I believe it spawns in the middle of the wall, game object. There are 4 walls limiting square play area and this object or “aim” only spawns at the centre of each, depending which is closer to raycast.
So I’m wondering how could I do something like you can see on the picture?
Turret rotates for 360 degrees and gun goes up and down. I’d always like to have an object or “aim” at the collision point between raycast and wall.

Thank you for your time.

Solution:
Well I’ve found a solution, that fixed my problem:

{
    Vector3 objectForward = transform.TransformDirection(Vector3.forward);
    Debug.DrawRay(transform.position, objectForward, Color.green);
    Ray ray = new Ray(transform.position, objectForward);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit))
    {
        if (hit.transform.tag == "Enviroment")
        {
            hitLoc = ray.GetPoint(hit.distance); 
        }
    }
    crosshair.transform.position = hitLoc;
}

I calculated the distance between raycast start and end. I used this distance at ray.GetPoint, which does a ray that’s as long as the distance between raycast start and end, which then spawns “aim” at that rays end location.

Again, thank you for your time.