Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Aug 29, 2017 at 03:15 AM by miramaslow for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by miramaslow · Aug 03, 2017 at 01:26 AM · c#cameracoordinatesspawning problemsz-axis

How to spawn GameObject exactly in front of another object?

Im working on a 3D game in Unity and I'm having a problem trying to spawn an object exactly in front of a wall in my game, like hanging a picture on it. I don't want the object to look like it's too far from the wall or overlapping the wall.

 public void SpawnPrefab(Transform prefab)
 {
     compSize = prefab.gameObject.GetComponent<Collider>().bounds.size.z;
     Vector3 mousePosition, targetPosition;
     InputManager inputManager;
     inputManager = prefab.gameObject.GetComponent<InputManager>();
     mousePosition = Input.mousePosition;
     targetPosition = Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x,mousePosition.y,mousePosition.z + compSize/2 + wallSize/2));
     prefab.localPosition = targetPosition;
     if(isAlreadyClicked == false)
     {
         GameObject comp = (GameObject) Instantiate(prefab.gameObject, new Vector3(prefab.transform.position.x, prefab.transform.position.y, prefab.transform.position.z + compSize/2 + wallSize /2), prefab.transform.rotation);
         isAlreadyClicked = true;
     }
 }

I tried adding float value on the mouseposition.z like mouseposition.z + but it doesn't seem working like how I wanted because I have different object with different sizes, where some objects spawn at the back of the wall, while some will spawn too far at the front from the wall.

Any help would be appreciated.

Thanks thanks thanks!

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 UnityCoach · Aug 03, 2017 at 04:32 AM 0
Share

You may want to look at Decals, so that your object is exactly at the position of the wall, yet appears on top. This is the technique for bullet holes and other impacts.

3 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by miramaslow · Aug 03, 2017 at 08:32 AM

Thanks for the replies, but I managed to get it done. Here's the full code

 public void SpawnPrefab(Transform prefab)
     {
         InputManager inputManager;
         clicked = false;
         compSize = prefab.gameObject.GetComponent<Collider>().bounds.size.z;
         inputManager = prefab.gameObject.GetComponent<InputManager>();
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, out hit))
         {
             Debug.Log("clicked");
             if(isAlreadyClicked == false)
             {
                 OnClickedButton();
                 Instantiate(prefab, new Vector3(hit.point.x, hit.point.y, frontWall.gameObject.transform.position.z + compSize*2 - wallSize*3), prefab.transform.rotation);
                 isAlreadyClicked = true;
                 Debug.Log(clicked);
             }
         }
     }


This is much more simpler solutions than what I did before. Source: this question , and this Unity question

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
avatar image
1

Answer by Alturis2 · Aug 03, 2017 at 01:38 AM

You are going to have to take your ScreenToWorldPosition() result and then run a RayCast collision trace out from that point forward and place your object at the resulting hit position minus the bounds of the object. Sounds like you will also want to use the resulting surface normal to define the rotation of the object so it aligns to it.

https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Comment
Add comment · Show 2 · 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 miramaslow · Aug 03, 2017 at 03:01 AM 0
Share

I'm using a button to spawn the object according to the mouseclick position on the screen, do I have to use plane raycast ins$$anonymous$$d? since my origin point is the button..

avatar image miramaslow · Aug 03, 2017 at 04:36 AM 0
Share

I managed to get the accurate z position for the spawning process, but there's something wrong with its x and y position, it's not spawning based on the mouseclick position.

 public void SpawnPrefab(Transform prefab)
 {
     clicked = false;
     compSize = prefab.gameObject.GetComponent<Collider>().bounds.size.z;
     Vector3 mousePosition, targetPosition;
     Input$$anonymous$$anager input$$anonymous$$anager;
     input$$anonymous$$anager = prefab.gameObject.GetComponent<Input$$anonymous$$anager>();
     mousePosition = Input.mousePosition;
     targetPosition = Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x,mousePosition.y,mousePosition.z));
     ray = Camera.main.ScreenPointToRay(targetPosition);
     float rayDistance;
     if(plane.Raycast(ray, out rayDistance))
     {
         prefab.localPosition = ray.GetPoint(rayDistance);
         Debug.Log("clicked");
         if(isAlreadyClicked == false)
         {
             OnClickedButton();
             GameObject comp = (GameObject) Instantiate(prefab.gameObject, new Vector3(targetPosition.x, targetPosition.y, frontWall.gameObject.transform.position.z + compSize - wallSize), prefab.transform.rotation);
             isAlreadyClicked = true;
             Debug.Log(clicked);
         }
     }
 }

I don't know what is happening..

avatar image
0

Answer by a161803398874 · Aug 03, 2017 at 05:00 AM

You can add an Empty GameObject and place it exactly where you want to spawn the objectc, we do this to keep track of the transform. In your script you add

    public Transform emptyObjectTransform;
 

to get hold of the values, you will need to drag and drop the empty object to the script in the inspector, no when you pass the spawn values in your "Instantiate" method you pass

 emptyObjectTransform.position

You can also pass rotation....

Dont recomend this method if you are going to spawn many items because you will need one emptygame object for each spawn pont

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

379 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Getting other cameras coordinates 1 Answer

Random position in rectangle, but not in a camera view 1 Answer

UNITY 3D: How to make the camera follow the player? Smoothly 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