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 /
avatar image
0
Question by richwebb8 · Jul 09, 2017 at 07:58 AM · androiddrag-and-dropphonesnapping

Problem snapping object on phone, but fine on PC.

I am developing for an android phone (Samsung S7) using google VR SDK for the cardboard.

I am trying to create a script that allows a cube to be dragged and dropped and then snapped to another cube when the objects are close enough. The script works perfectly when on the computer.

When I build and run on my phone, the cube can be dragged and dropped. However, the cube does not snap to the position of the second cube when close.

I have attached the whole script but I think it is the last if statement that is screwing up:

  using UnityEngine;
  using System.Collections;
  
  public class DDCube : MonoBehaviour
  {
      private Vector3 screenPoint;
      private Vector3 offset;
      Ray ray;
      float distance = Mathf.Infinity; //Infinite Distance
  
      public void Update()
      {
          int layer_mask = LayerMask.GetMask("Cube");
  
          ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.0f)); //Centre of the screen (i.e. reticle)
          if (Physics.Raycast(ray, distance, layer_mask)) //Only hit if Cube
          {
  
              //At the frame the screen is touched
              if (Input.GetMouseButtonDown(0))
              {
                  screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position); //Where is the reticle in screen space?
                  offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); //How far is the game object from reticle?
              }
  
              //Executed each frame whilst the screen is being touched
              if (Input.GetMouseButton(0))
              {
                  Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
                  Vector3 cursorPosition;
                  //Stop cube from moving outside boundary
                  cursorPosition.x = Mathf.Clamp(Camera.main.ScreenToWorldPoint(cursorPoint).x + offset.x, -4f, 4f);
                  cursorPosition.y = Mathf.Clamp(Camera.main.ScreenToWorldPoint(cursorPoint).y + offset.y, 0.5f, 4f);
                  cursorPosition.z = Mathf.Clamp(Camera.main.ScreenToWorldPoint(cursorPoint).z + offset.z, -4f, 4f);
                  gameObject.transform.position = cursorPosition; //Move cube to reticle position
                  GetComponent<Renderer>().material.color = Color.blue; //Change cube colour to blue
              }
          }
  
          //At the frame the touch is released
          if (Input.GetMouseButtonUp(0))
          {
              GetComponent<Renderer>().material.color = Color.red; //Change cube colour to red
              GameObject Cube1 = GameObject.FindWithTag("Cube1");
              GameObject Cube2 = GameObject.FindWithTag("Cube2");
              var dist = Vector3.Distance(Cube2.transform.position, Cube1.transform.position);
              var minDist = 2f;
  
              //Snap Cube to Position
              if (dist < minDist)
              {
                  Cube1.transform.position = Cube2.transform.position;
                  Cube1.transform.rotation = Cube2.transform.rotation;
                  GetComponent<Renderer>().material.color = Color.green;
              }
  
          }
      }
  }

Thanks for the help, I am very new to unity so sorry if the script is structured horribly or anything.

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

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

132 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

Related Questions

Certificate problems when installing APK file on Android phone from command line 1 Answer

Game wont open on android phone 1 Answer

Sound Quality and Lag when using WebRTC with Unity on LG Phones 0 Answers

UIButtons and UITexts are on the good position when I run the app with Remote4 but when I install .apk it's totoally different. How to solve it? 1 Answer

Porting Unity Game to Android 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