Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by mmuller · Dec 21, 2010 at 10:25 PM · javascriptinstantiatemeshvector3vertex

instantiate object and align with object surface

Hello,

I have a game with a level map and a script that generates objects randomly in a circle above it. Is there a way I could have the object actually use the surface objects y coordinate rather than floating down on to it from above? The surface object is not a flat surface you see.

I am guessing that I need to find the mesh vertices from the object I am wanting to stick my new object to, but am unsure how to go about it.

Any help greatly appreciated.

mmuller

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

2 Replies

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

Answer by Jesse Anders · Dec 22, 2010 at 05:49 PM

If the surface has a collider associated with it, you could perform a vertical raycast to find the y coordinate that you're looking for.

Comment
Add comment · 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 mmuller · Dec 22, 2010 at 09:09 PM 0
Share

The map does indeed have a mesh collider associated to it, I will have a read up on raycasts - never had a cause to use them... until now it seems :)

avatar image
2

Answer by mmuller · Dec 22, 2010 at 10:46 PM

For anyone that wants it or might find it useful this is the script that I attached to the prefab as it is instantiated, which then aligns it to the level map:

    var hit : RaycastHit;
    var down = Vector3(0,-1, 0);
if (Physics.Raycast (transform.position, down, hit)) {
    var distanceToGround = hit.distance;
    var currentPos = transform.position;
    var newY = currentPos.y-distanceToGround;
    transform.position = Vector3(currentPos.x, newY, currentPos.z);
}

This basically grabs the objects initial starting position (out of sight in my case at +1000 y and then instantly alters it to the underlying objects height by subtracting this distance from its initial position.

Thanks to Jesse for the pointer in using Raycasting to find the Vector3 of the object to stick to.

Comment
Add comment · 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 jjohn8521 · Apr 14, 2018 at 03:59 PM 0
Share

Hey so I have read your question about instantiating objects but I am having trouble with my script still. I am trying to make an RTS style game and i can't get the instantiated objects to the mouse location or to stick to a sphere. ($$anonymous$$y world is a sphere and the player walks around it.) What I intent is the player clicks a UI button, then drags it to a location and when the player releases the mouse they will place the object at the location where they released the mouse button. Here is my code so far which doesn't work:

     using System.Collections;
     using UnityEngine;
     
     public class BuildingPlacement : $$anonymous$$onoBehaviour {
     
         private Transform currentBuilding;
     
     
         // Use this for initialization
         void Start () {
             
         }
         
         // Update is called once per frame
         void Update () {
     
             if (currentBuilding != null) {
                 Vector3 m = Input.mousePosition;
                 m = new Vector3(m.x,m.y, transform.position.y);
                 Vector3 p = Camera.main.ScreenToWorldPoint(m);
                 currentBuilding.position = new Vector3 (p.x, 0, p.z);
             }
     
         }
     
         public void SetItem(GameObject b) {
             currentBuilding = ((GameObject)Instantiate(b)).transform;
         }
     }
 
 



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

1 Person is following this question.

avatar image

Related Questions

Help with my Flocking Algorithm 0 Answers

Avoiding instantiating two things in same location? 1 Answer

When generating a procedural voxel mesh, i get something weird. 1 Answer

How can I find the terminal edges on a mesh, after removing triangles from it? 0 Answers

I keep getting: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(float, UnityEngine.Vector3, UnityEngine.Quaternion)' was found. 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