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 DavidDebnar · Jul 08, 2011 at 08:13 AM · instantiateprefabraycastingspread

Raycasting

Hey guys! I don't have a clue what am I doing wrong...Simply. I need to raycast into the Z direction, than if there is nothing I need to instantiate a cube and if there is something I need to check the Y and -Y and X and -X and if there is nothing again create cubes at the sides...I did raycasting like XY times...but I just can't figure this one out. And that cubes are the same so they will again check the same thing, so it will something like "spread" :).

Spreading

var prefab : Transform;

var rayRange : float = 2; var mask : LayerMask;

var canPlace : boolean;

var right : boolean; var left : boolean; var up : boolean; var down : boolean; var someThingIsBelow : boolean = true;

function Start () { prefab = transform; }

function Update () { Bottom(); Place(); } function Bottom() { var hit : RaycastHit;

 if(Physics.Raycast(transform.position, -transform.forward, hit, rayRange))
     if(hit.collider.tag != "waterBlock")
     {
         someThingIsBelow = true;
         canPlace = true;
         CheckSides();
     }
     else
         canPlace = false;
 else
 {
     someThingIsBelow = false;
     canPlace = true;
 }

}

function CheckSides() { if(canPlace) { var sideHit : RaycastHit;

     if (Physics.Raycast(transform.position, transform.up, sideHit, rayRange)) up = false;
     else up = true;

     if (Physics.Raycast(transform.position, -transform.up, sideHit, rayRange)) down = false;     
     else down = true;
     
     if (Physics.Raycast(transform.position, transform.right, sideHit, rayRange)) right = false;
     else right = true;
     
     if (Physics.Raycast(transform.position, -transform.right, sideHit, rayRange)) left = false;
     else left = true;
 }

}

function Place() { if(canPlace) { if(!someThingIsBelow) DoBlock(Vector3(0,0,1));

     if(someThingIsBelow)
         if(right) DoBlock(Vector3(1,0,0));
         if(left) DoBlock(Vector3(-1,0,0));
         if(up) DoBlock(Vector3(0,1,0));
         if(down) DoBlock(Vector3(0,-1,0));    
 }    

}

function DoBlock(offset : Vector3) { var myPos = transform.position; Instantiate(prefab, myPos + offset, transform.rotation); }

  • David

Comment
Add comment · Show 2
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 Lab013 · Jul 08, 2011 at 09:06 AM 0
Share

If you could post the code you're using that would be good.

avatar image DavidDebnar · Jul 08, 2011 at 09:24 AM 0
Share

I added the image, that how i want it to basically spread.

1 Reply

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

Answer by Joshua · Jul 08, 2011 at 10:40 AM

First of all, transform.TransformDirection( Vector3.forward ) is the same as transform.forward, same goes for up and right. Second of all in your if( tag != "waterblock") block of code you check the first raycast if there is not something above it, but then for underneath and next to you check if there is something. Add a '!'. And lastly, you write

 var myPos = Vector3(transform.position.x,transform.position.y,transform.position.z);

where you could just write

 var myPos = transform.position;

Fix the raycasting and see if that fixes it.

Comment
Add comment · Show 10 · 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
avatar image DavidDebnar · Jul 08, 2011 at 12:19 PM 0
Share

thanks I havent seen, that I dont have the ! at the other 3 Raycasts...ill try it

avatar image DavidDebnar · Jul 08, 2011 at 12:23 PM 0
Share

Nope...what is it doing is, that the first cube places another under itself and does it to infinite... like places the same block at the same place nothing else...

avatar image Joshua · Jul 08, 2011 at 12:54 PM 0
Share

Well to be honest I don't like the logic setup of how you do it. Don't do this in the Update() function because you want it run once per cube, not every frame for every cube. Second of all, your coding this from the perspective of the cubes, code it form the perspective of a masterscript, then use for and while loops to iterate trough all the blocks and add to them.

avatar image DavidDebnar · Jul 08, 2011 at 01:00 PM 0
Share

reason why i have it in update function is, that it will be water, that is flowing... well yeah, i could place it into start function , or use a masterscript... ill try something out...

avatar image Joshua · Jul 08, 2011 at 01:03 PM 0
Share

Well you can keep checking it continuously, but that will get quite expsive pretty soon with your current setup. For that a masterscript iterating through them all every (few) frames would also be better.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiating prefabs, when there is nothing 2 Answers

Physics.RayCast problem with instantiated prefab 0 Answers

Changing LocalScale in code affecting prefab stored in Assets 1 Answer

Instantiate a prefab vs create one dynamically at runtime? 1 Answer

Spawning a prefab at another object's location 3 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