• 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 mealone · Dec 22, 2014 at 05:39 AM · movementtransformpositiondragclick

how can do click then move on a gameobject?

Hello everyone.

This is what i am trying to do : click the gameobject, and after clicking, a directional arrow will appear (up, down, left, or right). The object has to be moved or dragged in that direction with the mouse. eg. directional arrow is right. The player will click the gamebject and move the mouse in the direction (right). This will indicate a right swipe or drag. The gameobject will be moved along the X axis by +1, but if the swipe/ drag is in any other direction but right, the gameobject will not move.

Here is the code that I am trying:

 var startpos : Vector3 = Vector3.zero;
 
 function Update () {
 if (Input.GetMouseButton(0) && !touched)
      {
        var hitm : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
        if (hitm.collider != null && hitm.collider.gameObject == circle)
        {
          var mouseDelta : Vector3 = Input.mousePosition - startpos;
         if(mouseDelta.x < -0.5){
         movedir = left;
          }
          if(mouseDelta.x > 0.5){ 
 movedir = right;
          }
 if(mouseDelta.y > 0.5){
 movedir = up;
          }
 if(mouseDelta.y < -0.5){
 movedir = down;
          }
 startpos = Input.mousePosition;
       }
    }
 }


This isn't giving the desired result. The gameobject is being moved with the transform script :

 if ( movedir == down){
 transform.position = Vector3.MoveTowards(transform.position,Vector3(transform.position.x,
 transform.position.y-1, transform.position.z ), 2* Time.deltaTime);
 
 }
 else if ( movedir == up){
 transform.position = Vector3.MoveTowards(transform.position,Vector3(transform.position.x,
 transform.position.y+1, transform.position.z ), 2* Time.deltaTime);
 
 }
 else if ( movedir == left){
 transform.position = Vector3.MoveTowards(transform.position,Vector3(transform.position.x-1,
 transform.position.y, transform.position.z ), 2* Time.deltaTime);
 
 }
 else if ( movedir == right){
 transform.position = Vector3.MoveTowards(transform.position,Vector3(transform.position.x+1,
 transform.position.y, transform.position.z ), 2* Time.deltaTime);
 
 }

Can anyone help with this?

Comment
Add comment · Show 2
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 mealone · Dec 22, 2014 at 06:52 AM 0
Share

this is a 2D game.

avatar image mealone · Dec 22, 2014 at 02:00 PM 0
Share

Anything on this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · Dec 22, 2014 at 06:15 AM

Is this the sort of thing you are looking for?

http://answers.unity3d.com/questions/12685/how-to-create-handles-like-editor-gizmos-in-runtim.html http://wiki.unity3d.com/index.php?title=Lipis_Runtime_Gizmo http://forum.unity3d.com/threads/rtgizmo-run-time-position-rotation-scale-gizmo.120007/

Comment
Add comment · Show 2 · 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 mealone · Dec 22, 2014 at 06:51 AM 0
Share

Hi, thanks for the response. Unfortunately, this is not what i'm looking for. I forgot to mention that this is 2D, and the gameobjects will be in a grid system (think 2048 in terms of the set up). I want that when the click occurs on one or any of the objects in the grid they can then swipe that gameobject into the direction of the arrow which is shown, be it right, left, up or down. I hope this helps to clarify what i mean.

avatar image mealone · Dec 23, 2014 at 03:29 AM 0
Share

Anything?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

27 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

Related Questions

Getting to move object with mouse position? 0 Answers

How to make object follow touch position?Pls Help 0 Answers

Add specific movement on double click 1 Answer

2D Background Stop Moving on BoundingBox Exit? 2 Answers

Move object with finger swipe 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges