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 it9760 · Oct 05, 2011 at 10:45 AM · move

Moving GameObjects with Mouse

Hallo!

How can I move an object (a cube for example) to a certain target point by clicking on the object and holding down the left mouse button?

Main idea is to move certain objects to their correct places.

Thanks in advance

Maria

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 Tseng · Oct 05, 2011 at 08:36 PM

I've used this one to move a crosshair in a 2.5D game.

It's a bit more complex, but it has two important things in it

  1. It correctly accounts for the offset (depending on where you touch, other scripts will place the object below your mouse cursor), so that the object is moved correctly instead of it's origin being placed below the mouse cursor

  2. It also has input touch, if you use it on Android/iPhone which doesn't react on OnMouseXXX Events

You can remove the Input parts of the controls, if you don't need them.

 using UnityEngine;
 using System.Collections;

 public class MoveObjectScript : MonoBehaviour {
     private Vector3 screenPoint;
     private Vector3 offset;
     private Vector3 curScreenPoint;
     
     void Update() {
         if(Input.touchCount==1) {
             Touch touch = Input.touches[0];
             
             Ray ray = Camera.main.ScreenPointToRay(touch.position);
             RaycastHit rayHit = new RaycastHit();
             bool isOverCrosshair = false;
             if(Physics.Raycast(ray, out rayHit, 20.0f, layer)) {
                 isOverCrosshair = rayHit.transform.name=="Crosshair";
             }

             switch(touch.phase) {
             case TouchPhase.Began:
                 if(isOverCrosshair)
                     OnMouseDown();
                 break;
             case TouchPhase.Canceled:
             case TouchPhase.Ended:
                 if(bMouseDown)
                     OnMouseUp();
                 break;
             case TouchPhase.Moved:
                 OnMouseDrag();
                 break;
             default:
                 break;
             }
         }
     }
     
     void OnMouseDown() {
         Vector3 position = Vector3.zero;
         if(Input.touchCount>0) {
             position = new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, 0);
         } else {
             position = Input.mousePosition;
         }
         // get the start position 
         screenPoint = Camera.main.WorldToScreenPoint(transform.position);
     
         // calculate the offset from the click point to the center of the cross hair
         offset = transform.position - Camera.main.ScreenToWorldPoint(
             new Vector3(position.x, position.y, screenPoint.z));
     }
     
     void OnMouseDrag() {
         // calculate new position
         Vector3 position = Vector3.zero;
         if(Input.touchCount>0) {
             position = new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, 0);
         } else {
             position = Input.mousePosition;
         }

         curScreenPoint = new Vector3(position.x, position.y, screenPoint.z);
         transform.position = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
         // The above one  is for a 2D/2.5D game, where the object will be drag in x and y positon, but be locked on z position.
         // For full 3d movement, use this line instead 
         // transform.position = Camera.main.ScreenToWorldPoint(position) + offset;
     }
 }
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Goes faster when moving to monster's transform 1 Answer

I get errorsfor my Click to Move Script 0 Answers

movement script 2 Answers

Hit problem unity 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