Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by twoxsu · Apr 05, 2017 at 10:47 PM · raycastterrainraycasthitphysics.raycastterraincollider

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: alt text

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[i].transform.position.x,spawnOzdob[i].transform.position.y+5,spawnOzdob[i].transform.position.z);
             switch(spawnOzdob[i].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?

error.jpg (214.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MarshallN · Apr 06, 2017 at 03:51 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast hits everything on the screen 2 Answers

Tree how to add a tag to it when it is apart of the terrain -1 Answers

Raycasthit results only 0 0 Answers

Raycast hit in OnDrawGizmos but not in Update 1 Answer

Raycasting Through Terrain 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges