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 /
This question was closed Feb 13, 2020 at 08:21 PM by ZachRoman121 for the following reason:

Other

avatar image
0
Question by ZachRoman121 · Sep 16, 2017 at 02:17 AM · placementsideplacing

Placing an object on the side of another object?

Hello! I am working on a placing system for a game i am creating

The objects are going to be different shapes and sizes from one another, And these objects are going to 3D I'm trying to set the position of the selector on the side of another object And then i am trying to round the position for more a a grid-like placement

So far i have this code:

     public static bool editMode = true;
     public GameObject test;
     static Material selectorG;
     static Material selectorR;
     public static GameObject selector;
     public static bool canPlace;
     public static bool snapping = true;
 
 //This function is being called in LateUpdate()
 if (Physics.Raycast (ray, out hit)) {
             Vector3 hitSide = hit.normal;
             selector.gameObject.SetActive (true);
             Mesh model = go.GetComponent<MeshFilter> ().sharedMesh;
             selector.GetComponent<MeshFilter> ().mesh = model;
             Vector3 newPos;
 
             // Setting the position of the selector
             // If snapping is disabled
             if (snapping == false) {
                 //This works
                 newPos = new Vector3 (hit.point.x + Mathf.Ceil (hitSide.x) / 2, hit.point.y + 0.001f + Mathf.Ceil (hitSide.y) / 2, hit.point.z + Mathf.Ceil (hitSide.z) / 2);
             // If snapping is enabled
             } else {
                 //This doesn't work.. Instead it floats off the ground, And if you put your mouse against the wall, It will float off the wall too
                 newPos = new Vector3 (Mathf.Round(hit.point.x + Mathf.Ceil (hitSide.x) / 2), Mathf.Round(hit.point.y + 0.001f + Mathf.Ceil (hitSide.y) / 2), Mathf.Round(hit.point.z + Mathf.Ceil (hitSide.z) / 2));
                 // And the other way of doing this..
                 //This doesn't work because it intersects the object it hits, And i want it to be rounded..
                 newPos = ray.origin + (ray.direction * hit.distance);
             }
             selector.transform.position = newPos;
             selector.transform.localScale = localS;
 
             //Setting the material accordingly
             if (canPlace) {
                 selector.GetComponent<Renderer> ().material = selectorG;
             } else {
                 selector.GetComponent<Renderer> ().material = selectorR;
             }
             //Rotation.. Ignore this
             if (Input.GetKeyDown ("q")) {
                 selector.transform.Rotate (Vector3.up * 90);
             }
             if (Input.GetKeyDown ("e")) {
                 selector.transform.Rotate (Vector3.up * -90);
             }
             // Placing the object when you click the mouse
             if (Input.GetMouseButtonDown (0) && canPlace) {
                 GameObject newObj = Instantiate (go);
                 newObj.transform.position = selector.transform.position;
                 newObj.transform.rotation = selector.transform.rotation;
             }
         } else {
             //Making the selector disappear when there is nothing the mouse is hitting nothing
             selector.gameObject.SetActive (false);
         }
     }

And from line 48 to 59 i am setting the position

             Vector3 hitSide = hit.normal;
             if (snapping == false) {
                 //This works
                 newPos = new Vector3 (hit.point.x + Mathf.Ceil (hitSide.x) / 2, hit.point.y + 0.001f + Mathf.Ceil (hitSide.y) / 2, hit.point.z + Mathf.Ceil (hitSide.z) / 2);
             // If snapping is enabled
             } else {
                 //This doesn't work.. Instead it floats off the ground, And if you put your mouse against the wall, It will float off the wall too
                 newPos = new Vector3 (Mathf.Round(hit.point.x + Mathf.Ceil (hitSide.x) / 2), Mathf.Round(hit.point.y + 0.001f + Mathf.Ceil (hitSide.y) / 2), Mathf.Round(hit.point.z + Mathf.Ceil (hitSide.z) / 2));
                 // And the other way of doing this..
                 //This doesn't work because it intersects the object it hits, And i want it to be rounded..
                 newPos = ray.origin + (ray.direction * hit.distance);
             }

All help is appreciated :)

Comment
Add comment · Show 1
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 tormentoarmagedoom · Sep 18, 2017 at 09:36 AM 0
Share

Good day @creepercraft187 !

I try to help a lot of peopple, in order to help me to help you, tell me whats exactly / what lines you have problems? What do you need?

Is your code working fine? At line 47 :

          if (Input.Get$$anonymous$$ouseButtonDown (0) && canPlace) {
              GameObject newObj = Instantiate (go);
              newObj.transform.position = selector.transform.position;
              newObj.transform.rotation = selector.transform.rotation;

I recommend you do to it in a single line, so the object instantiate directly at the corect position

 GameObject newObj = Instantiate (go, selector.transform.position, selector.transform.rotation);

But is just a little thing... Tell me where is the problem.

Bye :D

1 Reply

  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Sep 19, 2017 at 07:41 AM

Hello @creepercraft187 !

Whats the problem ? I didnt read the code (so long and i dont kno what you need). Whats the question? I can give you "general examples" of code for something, but i dont know what you want exactly.

Ask/Give info using @tormentoarmagedoom

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

Follow this Question

Answers Answers and Comments

69 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

Related Questions

How to make the turret not placeable on walls? 1 Answer

Place an object on the side of another object 0 Answers

Placing an object with limits 1 Answer

How do I place buttons on the screen using GUITexture? 3 Answers

Check if objects are placed in a certain shape 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