• 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
Question by klisman · Jun 28, 2017 at 01:04 PM · c#collisionraycast

Align to grid in runtime problem

I'm trying to create a building system following a tutorial, the object is only positioned in the first row.

Demo of the problem

Tutorial i'm using

Align code

     public void StartPreview()
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out $$anonymous$$t, 10, rayCastLayers))
         {
             if ($$anonymous$$t.transform != t$$anonymous$$s.transform)
             {
                 ShowPreview($$anonymous$$t);
             }
         }
     }
 
     public void ShowPreview(RaycastHit $$anonymous$$t2)
     {
         currentPosition = $$anonymous$$t2.point;
         currentPosition -= Vector3.one * currentObject.size;
         currentPosition /= currentObject.size;
         currentPosition = new Vector3(Mathf.Round(currentPosition.x), Mathf.Round(currentPosition.y), Mathf.Round(currentPosition.z));
         currentPosition *= currentObject.size;
         currentPosition += Vector3.one * currentObject.size;
         currentPreviewTransform.position = currentPosition;
 }


Comment

People who like this

0 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 ShadyProductions · Jun 28, 2017 at 02:09 PM 0
Share

Do you get any error inside the console?

avatar image klisman ShadyProductions · Jun 28, 2017 at 02:13 PM 0
Share

No, for some reason i can only place the first row,

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by toddisarockstar · Jun 29, 2017 at 02:16 PM

I am guessing that possibly your camera may be too close and and clipping the display of the top cubes?

anyways seems you have much more in the code than what you need i cant figure out why you have all those extra calculations. All you should need for placement is the object's transform position and simply add the height. Thats all!!!

also your raycast is finding the cube that the mouse is $$anonymous$$tting. i t$$anonymous$$nk what you really want is the $$anonymous$$ghest cube of the stack you are pointing at. so i reccomend a secondary raycast to find the $$anonymous$$ghest cube of the stack. i wrote t$$anonymous$$s to recreate your problem. Run t$$anonymous$$s in a new empty scene:

     int i;
     int i2;
     RaycastHit $$anonymous$$t;
     RaycastHit $$anonymous$$t2;
     GameObject preview;
     GameObject g;
     Vector3 v;
     public void Start(){
         // create a floor for example purpose;
         preview = GameObject.CreatePrimitive(PrimitiveType.Cube);
         preview.collider.enabled = false;
         preview.renderer.material.color = Color.red;
         i = 20;
         w$$anonymous$$le(i>0){i--;
             i2=20;
             w$$anonymous$$le(i2>0){i2--;
                 g = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 g.transform.position=new Vector3(i,0,i2);
                 g.transform.renderer.material.color = Color.green;
             }}
         Camera.main.transform.position = new Vector3 (9, 8, 0);
         Camera.main.transform.eulerAngles = new Vector3 (52, 0, 0);
     }
     public void Update()
     {           Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                 if (Physics.Raycast (ray, out $$anonymous$$t, 100)) {
                         v=$$anonymous$$t.transform.position + new Vector3(0,100,0);
          // second raycast to find the top cube
                         Physics.Raycast(v,-Vector3.up,out $$anonymous$$t);
                    // t$$anonymous$$s next one line is all you need to calculate your position!!!!!!
                         preview.transform.position=$$anonymous$$t.transform.position + new Vector3 (0, $$anonymous$$t.transform.localScale.y, 0);
             if (Input.GetMouseButtonDown (0)) {
             g = GameObject.CreatePrimitive(PrimitiveType.Cube);
             g.transform.position=preview.transform.position;
             g.renderer.material.color = Color.blue;
             }}}





Comment
klisman

People who like this

1 Show 1 · 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 klisman · Jun 29, 2017 at 09:58 PM 0
Share

With your help i found the problem, but not the solution yet, the problem is in the math that you say, with your test i was able to queue many blocks thanks you, i will try change to build in any side.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

357 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 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 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 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

How to check if a raycast is not hitting any tagged colliders? 0 Answers

Raycast doesnt detect object in front of rigidbody (player is stuck on wall) 1 Answer

Raycast won't fire 3 Answers

Dashing through enemies, but not walls. 1 Answer

Multiple Cars not working 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