Rays do not detect terrain and prefabs do not spawn where wanted

Hello, i have created script which instantiates prefab on maps on all possible spawnpoints. It searches for all spawnpoints and puts them on List. Then, it calls a function which casts a ray downwards from position of spawnpoint in search for suitable terrain to spawn tree. There are few things that may fail, but i always want it to spawn few trees on one spawnpoint, 3 for example. This will enforce it to cast 3 times, but it might fail and not hit terrain. But it does hit 99% times, so it is not a big problem if some spawnpoints creates only 2 trees. The problem is, that even if 99% of the rays hit the ground, most of them do not make contact. I used Debug.Line to demonstrate this:

The things on mountain are trees which are supposed to spawn on the point of contact with terrain.
My code (code might miss a brace or two because i needed to trim out long commented areas and i might have trimmed too much):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class raySpawn : MonoBehaviour {
//public GameObject[] propy = new GameObject[30];
public GameObject [] brzozy = new GameObject [5];
public GameObject[] kamole = new GameObject[10];
private Vector3 origin;
private int index = 0;
private bool czyIndexPoprawny = false;
public GameObject parent;
private int odl = 0;
public int iloscPropow = 90;
public int iloscSkał = 2000;
private int zrespionePropy = 0;
	// Use this for initialization
	void Start () {
		SkierujZwierzeta(); //DOESNT MATTER
		origin = gameObject.transform.position;
		RaycastHit hit;
		List<GameObject> spawnOzdob = new List<GameObject>();
		GameObject[] spawnOzdobTable;
		spawnOzdobTable = GameObject.FindGameObjectsWithTag("SpawnOzdoby");
		spawnOzdob.AddRange(spawnOzdobTable);
		//PROPY
		for(int i = 0; i < spawnOzdob.Count; i++)
		{
			Vector3 origin = new Vector3(spawnOzdob<em>.transform.position.x,spawnOzdob_.transform.position.y+5,spawnOzdob*.transform.position.z);*_</em>

_ switch(spawnOzdob*.name)
{
default:
Debug.Log(“Nie wykryto żadnych spawnów pod akceptowanymi nazwami”);
break;
case “SpawnBrzozy”:
SpawnujWColliderze(origin, brzozy, Random.Range(0,5),10);
break;
}
}
}*_

* void FixedUpdate(){ //DOES NOT MATTER IN THIS CASE*
* odl++;*
* if(odl == 3000)*
* {*
* odl = 0;*
* SkierujZwierzeta();*
* }*
* }*
* void SpawnujWColliderze(Vector3 origin, GameObject[] zbiór, int index, int gęstość)*
* {*
* Debug.Log(“Przygotowuje sie do spawnowania spawnOzdob”);*
* RaycastHit hit;*
* for(int i = 0; i < gęstość;i++)*
* {*
* Vector3 cel = new Vector3(origin.x+Random.Range(-16,16),origin.y-10,origin.z+Random.Range(-16,16));*
* Debug.DrawLine(origin,cel,Color.red,1000);*
* Ray linia = new Ray(origin, cel);*
* if(Physics.Raycast(linia, out hit))*
* {*
* Debug.Log(“Raycast wystrzelony”);*
* if(hit.transform.tag == “ZaczepBudowy”)*
* {*
* Debug.DrawLine(origin,cel,Color.green,1000);*
* //i++;*
* Debug.Log(“Zespawnowałem ozdobę”);*
* //zrespionePropy++;*
* GameObject obiekt = Instantiate(zbiór[index],hit.point,Quaternion.identity) as GameObject;*
* obiekt.AddComponent<UnityEngine.AI.NavMeshObstacle>().carving = true;*
* obiekt.transform.parent = parent.transform;*
* }*
* else if(hit.transform.tag != “ZaczepBudowy”)*
* {*
* //i–;*
* }*
* }*
* }*
* }*
* void SkierujZwierzeta(){ //COMPLETELY DOESNT MATTER IN THIS PROBLEM*
* Debug.Log(“Przesylam pozycje ruchu”);*
* RaycastHit hit;*
* Ray linia = new Ray(gameObject.transform.position, new Vector3(Random.Range(-1000,1000), -500, Random.Range(-1000,1000)));*
* if(Physics.Raycast(linia,out hit) && hit.transform.tag == “ZaczepBudowy”)*
* {*
* if(hit.point.y > 24)*
* {*
* GameObject.FindGameObjectWithTag(“Zwierze”).SendMessage(“ZmienCel”,hit.point,SendMessageOptions.DontRequireReceiver);*
* }*
* }*
* else*
* SkierujZwierzeta();*
* }*
}
I assume that the problem is related to how my code handles rays, but i dont know. Can someone help me?

When creating a ray, it takes two arguments - position of the beginning of the ray, then direction the ray should go in. Your Vector3 ‘cel’ is a positional vector, not a direction, but you’re putting it into the ray as if it’s a direction. If you insert the following line Vector3 celDirection = cel - origin; just before you cast the ray, then use Ray linia = new Ray(origin, celDirection); instead of Ray linia = new Ray(origin, cel);, it should be fine.

Those Debug.DrawLines aren’t drawing the actual rays, because of this error. If you draw the line between origin and hit.point, it’ll show you the actual ray.