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
0
Question by ZXYNINE · Dec 03, 2014 at 03:36 AM · builddrag-and-droprocket

Building in Unity

Alright so i have a drag and drop script, a gui that makes parts, and the parts that i want to go together but i dont know how to make it so that if you drag the part close enough to a cretin spot it will snap t it and if i let go of click it will place it there because im making a game with similar building mechanics to Kerbal space program can anyone help me with that?

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 Mmmpies · Dec 03, 2014 at 08:58 PM

No idea about Kerbal or cretin spots but if you have a collider on the part you want to snap to and a collider on the part you to snap in that place you should be able to do an onMouseUp event that then sets the transform of the snapto part to = the transform of the part you want it snap to. Man that's a mouthful!

Or, having reread your post!, when the colliders collide snap one part to the other. All you really need are two empty game objects one in each part and then set the transform of the one you want to snap in place to equal the transform of the one you want it to snap to. Just place the empty game objects in the models so when they match up they look right.

I've not tried this myself but the theory is sound, if you're not sure how to do that let me know and see if I can put a script together as it's not 100% different from attaching a sword or gun to a characters hand. I only work in C# though.

Oh and just look up colliders and attaching a weapon with the word unity in the search on YouTube and I'm pretty sure you'll find exactly what you need.

Comment
Add comment · Show 9 · 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 ZXYNINE · Dec 03, 2014 at 10:37 PM 0
Share

I have no clue where to start with this stuff so thanks for the help i also found this which you might want to look at befor trying to help http://docs.unity3d.com/ScriptReference/Vector3.Distance.html

avatar image Mmmpies · Dec 04, 2014 at 09:55 PM 0
Share

Yeah I know about Distance, a collider is really just that. A box or sphere or other the checks if the distance has been breached.

I've got a basic setup but it looks like I can't upload a unity package.

There are 3 scripts. One on the higher object (I watched the vid on your other post after you commented on my question) and one on the lower and one from another post on here that goes on both.

It's not exactly what you want but hopefully will give you head start.

so on the upper object like the capsule:

 using UnityEngine;
 using System.Collections;
 
 public class SnapTopToBottom : $$anonymous$$onoBehaviour {
 
     public GameObject SnapBottom;
     public GameObject $$anonymous$$e;
     private Transform otherSnapTop;
     private TopSnap otherTopSnapScript;
 
     void OnTriggerEnter(Collider other)
     {
         Debug.Log (name + " tiggered " + other.name + " " + other.transform);
 
         otherTopSnapScript = other.GetComponent<TopSnap>();
         otherTopSnapScript.SnapToBottom($$anonymous$$e);
     }
 
 }

then on the lower object (like fuel tanks)

 using UnityEngine;
 using System.Collections;
 
 public class TopSnap : $$anonymous$$onoBehaviour {
 
     public GameObject SnapTop;
     public float offSet;
     private Transform tempTrans;
     private bool Snap = false;
     private SnapTopToBottom sTTBscript;
 
     void Update()
     {
         if(Snap)
         {
             tempTrans = sTTBscript.SnapBottom.transform;
             SnapTop.transform.position = new Vector3(tempTrans.position.x, tempTrans.position.y + offSet, tempTrans.position.z);
         }
     }
 
     public void SnapToBottom(GameObject Bottom)
     {
         Snap = true;
         sTTBscript = Bottom.GetComponent<SnapTopToBottom>();
     }
 }

Finally on both:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof($$anonymous$$eshCollider))]
 
 public class GizmosController : $$anonymous$$onoBehaviour 
 {
     
     private Vector3 screenPoint;
     private Vector3 offset;
     
     void On$$anonymous$$ouseDown()
     {
         screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
         
         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
         
     }
     
     void On$$anonymous$$ouseDrag()
     {
         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
         
         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
         transform.position = curPosition;
         
     }
     
 }

shame I can't upload the unity package so you can see that, let me know if you know a way.

avatar image Mmmpies · Dec 04, 2014 at 10:03 PM 0
Share

PS this is a short vid of it working. I've seen people upload unity packages to youtube but can find no tutorials on how to do that!

Snap to transform in Unity

avatar image ZXYNINE · Dec 05, 2014 at 02:04 AM 0
Share

Tanks for all the help you are awesome and if you add me on skype then you can sent files and it would make it easer to talk my skype is zxynine just so you know there are two zxynine's they are both me so just send a message to both

avatar image Mmmpies · Dec 05, 2014 at 05:56 PM 0
Share

Had issues with Skype but just got a dropbox account so I can share this:

SnapToFirstDraft

Note it's not a full solution so it will need work, but it'll give you a head start.

Can I ask a question, well I guess I just did but are you new to Unity and program$$anonymous$$g?

If so then my advice would be go on youtube and search for beginners guide to unity and to unity c# (or java if you prefer).

I'm not trying to be nasty it's just you'll get a lot more people helping if you've attempted the code in the first place.

Show more comments

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

26 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

Can the Samsung Galaxy Prevail be used with Unity? 0 Answers

My Mac Build doesn't Work 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