Raycast questions

I have a few questions about raycasting I’m using a raycast script for my gun heres the code.

    Screen.lockCursor = true;
    function Update () {
// Put this in your update function
    if (Input.GetButtonDown("Fire1")) {
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit : RaycastHit;
        if (Physics.Raycast (ray, hit, 100)) {
            print ("Hit something");
        }
    }
}

I have a few questions though.

  1. How would I use a texture as a crosshair in the center of the screen?
  2. How would I make the enemys react to the raycast hitting them would it be something like “OnRaycastEnter()”?
  3. How would I make the raycast go in the direction of the crosshair texture instead of the mouse?
  1. A texture as a crosshair is just a GUITexture in the middle of your screen. simple as that. Here’s how you use it:

http://unity3d.com/support/documentation/Components/class-GuiTexture.html

  1. The answer is in the code you posted. if your raycast hits something, it will drop into the conditional statement that says print(“Hit something”). from there you can access the object that it hit using hit.collider (what you do with it after that is up to you). Here’s how you use raycasting:

http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

  1. use Camera.ViewportPointToRay to create your ray:

http://unity3d.com/support/documentation/ScriptReference/Camera.ViewportPointToRay.html

You’ll notice that I linked the Unity docs in every answer. All of your questions were solved by one source of information. Please read the unity docs, you will learn a ton from it.