Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Thewoxyz · Jul 18, 2013 at 03:13 PM · instantiateraycast

Raycast not working with instantiated objects.

Hello, this is my first question on the forums. I was looking all over the internet,but I found no answer. I am making a game, which should generate land as you walk. Heres my code: var land : GameObject; var xpos : float; var zpos : float; var xuse : float; var zuse : float;

 function Update () {
 xpos = transform.position.x / 50;
 zpos = transform.position.z / 50;
 xuse = Mathf.Round(xpos) * 50;
 zuse = Mathf.Round(zpos) * 50;
 var hit : RaycastHit;
 
 if (!Physics.Raycast (Vector3(xuse,1,zuse), Vector3(xuse,-1,zuse), hit, 5.0)) {
         Instantiate(land,Vector3(xuse,0,zuse),Quaternion.identity);
         print("Land instantiated");
     }
 else {
     if (hit.transform.tag == "Land") {
         print("Land found");
         }
     }
 }

This might not be most efficent way to do it, but anyways. So when I walk it generates ground below me on a grid of 50. The problem is when it raycasts and finds nothing below it generates ground, but it doesnt detect it with raycast so it keeps infinitley generating it. I found on another post that checking for tags, but this isn't working since its kind of opposite. I really have no idea what to do. Waiting for any answers.

Comment
Add comment · Show 6
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
avatar image Dave-Carlile · Jul 18, 2013 at 03:14 PM 0
Share

Does the instantiated land object have a collider?

avatar image robertbu · Jul 18, 2013 at 03:15 PM 0
Share

Does the new land you create have a collider? Raycast() only works with objects that have colliders. If it does have a collider, then the next thing is to do a Debug.DrawRay() to verify that your Raycast() is going where you expect it to.

avatar image Thewoxyz · Jul 18, 2013 at 03:23 PM 0
Share

Yes the object has a collider. I just tested, and the raycast is going somewhere crazy.. hmm.. Thanks ! I'll try to figure it out now. I'll say if I need anymore help.

avatar image perchik · Jul 18, 2013 at 03:27 PM 0
Share

Can you explain your logic a little bit? What is xuse and yuse [conceptually] ?

avatar image Thewoxyz · Jul 18, 2013 at 03:28 PM 0
Share

Okay, I looked at code, and I have no idea. The thing is that it should be working cause they both have same position set except Y, but for some reason it now has raycast start from x50 y1 z0 but go to x100 y-1 z0. Thats weird cause they both have the same x set.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Thewoxyz · Jul 18, 2013 at 07:02 PM

Okay, all fixed :) Thanks to robertbu, I tried using Debug.DrawRay and I found out my ray was facing completely wrong direction. Everything is fixed now. If anyone is interested in the script then here ya go: var land : GameObject; var xpos : float; var zpos : float; var xuse : float; var zuse : float; var tr : Vector3;

 function Update () {
 xpos = transform.position.x / 50;
 zpos = transform.position.z / 50;
 xuse = Mathf.Round(xpos) * 50;
 zuse = Mathf.Round(zpos) * 50;
 tr = Vector3(xuse,1,zuse);
 var hit : RaycastHit;
 
 if (!Physics.Raycast (tr, Vector3(0,-1,0), hit, 5.0)) {
         Instantiate(land,Vector3(xuse,0,zuse),Quaternion.identity);
         print("Land instantiated");
         Debug.DrawRay(tr, Vector3(0,-1,0), Color.white, 5.0);
     }
 else {
     print("Something in the way");
     Debug.DrawRay(tr, Vector3(0,-1,0), Color.white, 5.0);
     }
 }
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

17 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

Related Questions

Instantiate a little before the end of the raycast or on screen 0 Answers

Instantiate Projector at Normal Direction 0 Answers

Raycast Specific Object/Instantiate Explosion 1 Answer

Instantiate prefab from original position to click mouse point position 0 Answers

Instantiate gameobject a certain distance away from transform 2 Answers


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