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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Feb 03, 2018 at 08:21 PM · translateblockside

Placing blocks Minecraft-style on round world?

Hello, I'm trying to make a game where blocks can be placed on the sides of other blocks like Minecraft, but on a giant sphere. The problem is that I can't figure out how to place blocks on whatever side of the other block was clicked on no matter the rotation. The code below will only work on the absolute "top" of the sphere.

                         var FrontDist = Vector3.Distance (HitResult.transform.gameObject.transform.position + new Vector3(0,0,-1), HitResult.point);
                         var BackDist = Vector3.Distance (HitResult.transform.gameObject.transform.position + new Vector3(0,0,1), HitResult.point);
                         var TopDist = Vector3.Distance (HitResult.transform.gameObject.transform.position + new Vector3(0,1,0), HitResult.point);
                         var BottomDist = Vector3.Distance (HitResult.transform.gameObject.transform.position + new Vector3(0,-1,0), HitResult.point);
                         var LeftDist = Vector3.Distance (HitResult.transform.gameObject.transform.position + new Vector3(-1,0,0), HitResult.point);
                         var RightDist = Vector3.Distance (HitResult.transform.gameObject.transform.position + new Vector3(1,0,0), HitResult.point);

                         Debug.Log (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist));
                         Debug.Log ("Front:" + FrontDist + "Back:" + BackDist + "Top" + TopDist + "Bottom" + BottomDist + "Left" + LeftDist + "Right" + RightDist);
                         Debug.Log ("Test:" + ChooseLowest (15, 10, 5));

                         if (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist) == FrontDist) {
                             PlacedItem.GetComponent<BlockScript> ().MoveAmount = new Vector3 (0, 0, -1);
                             Debug.Log ("Front");
                             PlaySound ("PlaceWood", (2.5f + Random.value)/3, 1.5f);
                         }
                         if (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist) == BackDist) {
                             PlacedItem.GetComponent<BlockScript> ().MoveAmount = new Vector3 (0, 0, 1);
                             Debug.Log ("Back");
                             PlaySound ("PlaceWood", (2.5f + Random.value)/3, 1.5f);
                         }
                         if (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist) == TopDist) {
                             PlacedItem.GetComponent<BlockScript> ().MoveAmount = new Vector3 (0, 1, 0);
                             Debug.Log ("Top");
                             PlaySound ("PlaceWood", (2.5f + Random.value)/3, 1.5f);
                         }
                         if (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist) == BottomDist) {
                             PlacedItem.GetComponent<BlockScript> ().MoveAmount = new Vector3 (0, -1, 0);
                             Debug.Log ("Bottom");
                             PlaySound ("PlaceWood", (2.5f + Random.value)/3, 1.5f);
                         }
                         if (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist) == LeftDist) {
                             PlacedItem.GetComponent<BlockScript> ().MoveAmount = new Vector3 (-1, 0, 0);
                             Debug.Log ("Left");
                             PlaySound ("PlaceWood", (2.5f + Random.value)/3, 1.5f);
                         }
                         if (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist) == RightDist) {
                             PlacedItem.GetComponent<BlockScript> ().MoveAmount = new Vector3 (1, 0, 0);
                             Debug.Log ("Right");
                             PlaySound ("PlaceWood", (2.5f + Random.value) / 3, 1.5f);
                         }
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

Answer by $$anonymous$$ · Feb 04, 2018 at 08:13 PM

I figured it out, rather than use something like [new Vector3(1,0,0)], I use [gameObject.transform.right], that way cube placement will be based on the rotation of the cube that it is being placed next to.

                         if (SelectedItem.GetComponent<BlockScript> ()) {
                             PlacedItem = Instantiate (SelectedItem, HitResult.transform.gameObject.transform.position, HitResult.transform.gameObject.transform.rotation);
 
                             var HitObject = HitResult.transform.gameObject;
 
                             var FrontDist = Vector3.Distance (HitObject.transform.position + HitObject.transform.forward, HitResult.point);
                             var BackDist = Vector3.Distance (HitObject.transform.position + -HitObject.transform.forward, HitResult.point);
                             var TopDist = Vector3.Distance (HitObject.transform.position + HitObject.transform.up, HitResult.point);
                             var BottomDist = Vector3.Distance (HitObject.transform.position + -HitObject.transform.up, HitResult.point);
                             var LeftDist = Vector3.Distance (HitObject.transform.position + HitObject.transform.right, HitResult.point);
                             var RightDist = Vector3.Distance (HitObject.transform.position + -HitObject.transform.right, HitResult.point);
 
                             if (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist) == FrontDist) {
                                 PlacedItem.GetComponent<BlockScript> ().MoveAmount = new Vector3 (0, 0, 1);
                                 PlaySound ("PlaceWood", (2.5f + Random.value)/3, 1.5f);
                             }
                             if (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist) == BackDist) {
                                 PlacedItem.GetComponent<BlockScript> ().MoveAmount = new Vector3 (0, 0, -1);
                                 PlaySound ("PlaceWood", (2.5f + Random.value)/3, 1.5f);
                             }
                             if (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist) == TopDist) {
                                 PlacedItem.GetComponent<BlockScript> ().MoveAmount = new Vector3 (0, 1, 0);
                                 PlaySound ("PlaceWood", (2.5f + Random.value)/3, 1.5f);
                             }
                             if (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist) == BottomDist) {
                                 PlacedItem.GetComponent<BlockScript> ().MoveAmount = new Vector3 (0, -1, 0);
                                 PlaySound ("PlaceWood", (2.5f + Random.value)/3, 1.5f);
                             }
                             if (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist) == LeftDist) {
                                 PlacedItem.GetComponent<BlockScript> ().MoveAmount = new Vector3 (1, 0, 0);
                                 PlaySound ("PlaceWood", (2.5f + Random.value)/3, 1.5f);
                             }
                             if (ChooseLowest (FrontDist, BackDist, TopDist, BottomDist, LeftDist, RightDist) == RightDist) {
                                 PlacedItem.GetComponent<BlockScript> ().MoveAmount = new Vector3 (-1, 0, 0);
                                 PlaySound ("PlaceWood", (2.5f + Random.value) / 3, 1.5f);
                             }
                         }
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

73 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

Related Questions

Input.GetMouseButtonDown() translates automatically to Mobile Platforms? 2 Answers

X-Axis Movement Using Transform Issue 0 Answers

Object reference not set to an instance of an object + array of positions 1 Answer

How do i simpy get the x position of an object inside the script attached to it, so i can use it to trigger an if event/print the value? 0 Answers

Rigidbody moving on platforms 0 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