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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by CorrectSpellingofSkilful · Dec 22, 2014 at 01:50 PM · parentattachmazemagnet

I need to be able to get an object to attach to another one on mouse hold

My game is a magnet maze, where you attach a magnet from underneath to a cube on top. It just needs to be below it to be able to able. There's an indicator set up to show when a cube is above the magnet but I don't yet have the code that enables the magnet to move the cube around wherever it moves.

This is my current code for the magnet:

using UnityEngine; using System.Collections;

public class Magnet : MonoBehaviour {

 public GameObject indicator;
 public Color attachableColour;
 public Color detachedColour;
 public CubeManager cubeManager;
 public GameObject magnetisedCube;
 public bool attaching;
 public bool attachable;
 public float attachRadius;
 private float distToCube;
 private GameObject closestCube;
 private float distToClosestCube;
 private Vector2 magPos;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () 
 {
     if (attaching)
     {
         magPos = new Vector2(transform.position.x, transform.position.z);

         foreach(GameObject cube in cubeManager.cubes)
         {
             Vector2 cubePos = new Vector2(cube.transform.position.x, cube.transform.position.z);

             distToCube = Vector2.Distance(magPos, cubePos);

             if (distToCube <= attachRadius)
             {
                 if (closestCube == null)
                 {
                     distToClosestCube = distToCube;
                     closestCube = cube;
                 }
                 else if (distToCube < distToClosestCube)
                 {
                     distToClosestCube = distToCube;
                     closestCube = cube;
                 }
             }
         }

         if (closestCube != null)
         {
             attaching = false;
             magnetisedCube = closestCube;
         }
     }
     else
     {
         attachable = false;

         magPos = new Vector2(transform.position.x, transform.position.z);

         foreach(GameObject cube in cubeManager.cubes)
         {
             Vector2 cubePos = new Vector2(cube.transform.position.x, cube.transform.position.z);
             
             distToCube = Vector2.Distance(magPos, cubePos);
             
             if (distToCube <= attachRadius)
             {
                 attachable = true;
                 break;
             }
         }

         if (attachable == false)
         {
             indicator.renderer.material.color = detachedColour;
         }
         else
         {
             indicator.renderer.material.color = attachableColour;
         }
     }

     if (attachable)
     {
         if(Input.GetMouseButtonDown(0))
         {
             attaching = true;
         }
     }
     else
     {
         magnetisedCube = null;
     }
 }

}

If someone can tell me what I need to add to this then I'd be very grateful.

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 Mmmpies · Dec 22, 2014 at 02:37 PM 0
Share

Lots of ways of doing this but it depends on your game layout.

Do you just want to be able to drag the object with the mouse?

I found a script on here a while ago that'll do it.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof($$anonymous$$eshCollider))]
 
 public class GizmosController : $$anonymous$$onoBehaviour 
 {
     
     private Vector3 screenPoint;
     private Vector3 offset;
     public bool IsDragable = true;
     
     void On$$anonymous$$ouseDown()
     {
         if(IsDragable)    // Only do if IsDraggable == true
         {
             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()
     {
         if(IsDragable)    // Only do if IsDraggable == true
         {
             Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
             
             Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
             transform.position = curPosition;
         }
     }
     
 }

Just put that on your cube.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Attach object to bone ingame. 1 Answer

How do i create a Magnet? 1 Answer

sphere animation magnet to another object 0 Answers

Attach object to avatar bone 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