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 /
  • Help Room /
avatar image
0
Question by acwdouglas · Oct 23, 2017 at 06:29 AM · 2d2d gamemove an object

How can I use a 'grabber' to move objects in Unity (2D)?

Hi, so I'm trying to use a grab box to move objects on a 2d grid. The grabber can move on the 8x8 grid, and will be able to select what is currently under it.

Are there any hints you can give as to how I can implement this? Right now all I have is a control built into my grabber object:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MovementScript : MonoBehaviour {
 
     public Vector2[][] myArray = new Vector2[8][];
     int yVal = 0;
     int xVal = 0;
 
     // Use this for initialization
     void Start () {
         print("Hello World");
         transform.position = new Vector3(0, 0, -10);
         for (int i = 0; i < 8; i++)
         {
             myArray[i] = new Vector2[8];
             for (int n = 0; n <8; n++)
             {
                 myArray[i][n] = new Vector2(i, n);
             }
         }
         print("Array initialized.");
     }
     
     // Update is called once per frame
     void Update () {
         
         if (Input.GetKeyDown("up"))
         {
             
             if (yVal < 7) {
                 yVal++;
                 transform.position = new Vector2(myArray[xVal][yVal].x, myArray[xVal][yVal].y);
             }
             
         }
         if (Input.GetKeyDown("down"))
         {
             if (yVal > 0)
             {
                 yVal--;
                 transform.position = new Vector2(myArray[xVal][yVal].x, myArray[xVal][yVal].y);
             }
         }
         if (Input.GetKeyDown("left"))
         {
             if (xVal > 0)
             {
                 xVal--;
                 transform.position = new Vector2(myArray[xVal][yVal].x, myArray[xVal][yVal].y);
             }
         }
         if (Input.GetKeyDown("right"))
         {
             if (xVal < 7)
             {
                 xVal++;
                 transform.position = new Vector2(myArray[xVal][yVal].x, myArray[xVal][yVal].y);
             }
         }
 
         
 
     }
 }
 

So right now just the basic movement functions.

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 Fockducker11 · Oct 23, 2017 at 10:22 AM

Your code make everything so complicate. Can you explain your code please? I can't understand at all? Is this a grabber script or the object?

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

181 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 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

How do I move a game object only on the x axis with arrow keys? 1 Answer

How to make bullet move? 0 Answers

Issues with dropping an object on multiple objects separetly - Simple 2d Drag & Drop 0 Answers

DontDestroyOnLoad not working properly on build (i think thats the problem atleast) 0 Answers

I can't do jump in my 2D game 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