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 Brutalitywarlord · Aug 18, 2016 at 07:13 PM · c#2d gamegridmove

Moving an object on a Grid

I am making a grid based Turn based game and i can't seem to be able to move the object anywhere, I can select the object and store it in a variable, but when i try to change it's transform.position, it doesn't work, I am a novice programmer so i haven't the slightest idea on how to make this work, My code will be shown below.

using UnityEngine; using System.Collections.Generic; using UnityEngine.UI; using System.Linq; using Random = UnityEngine.Random;

public class PlayerBehaviour : MonoBehaviour { public int ActionsPerTurn; public Boardmanager boardManager; private Vector3 SelectedPosition; private GameObject SelectedItem; private GameObject Mbutton; private GameObject Bbutton; private GameObject Abutton; //Returns the type of object at a clicked position GameObject ClickSelect() { RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

     if (hit)
     {
         return hit.transform.gameObject;
     }
     else return null;
 }
 Vector3 ClickPosition()
 {
     // collects the world position of a mouse click
     Vector3 Position = new Vector3 (Camera.main.ScreenToWorldPoint(Input.mousePosition).x,Camera.main.ScreenToWorldPoint(Input.mousePosition).y, 0f);
     return Position;
 }
 void Start () {
     boardManager = GetComponent<Boardmanager> ();
     Mbutton = GameObject.FindGameObjectWithTag("SpawnMSoldier");
     Abutton = GameObject.FindGameObjectWithTag("SpawnASoldier");
     Bbutton = GameObject.FindGameObjectWithTag("SpawnBSoldier");
     Mbutton.gameObject.SetActive(false);
     Bbutton.gameObject.SetActive(false);
     Abutton.gameObject.SetActive(false);
 }
 void Update () {
     if (Input.GetMouseButtonDown(0))
     {
         SelectedItem = ClickSelect();
         Debug.Log(ClickSelect());
         if (SelectedItem == GameObject.FindGameObjectWithTag("PlayerHQ"))
         {
             Mbutton.gameObject.SetActive(true);
             Bbutton.gameObject.SetActive(true);
             Abutton.gameObject.SetActive(true);
         }
         // if the object clicked was a Marksman
         else if(SelectedItem == GameObject.FindGameObjectWithTag("PlayerSoldierMarksman"))
         {
             //if the mouse is clicked down after the object is selected
             if (Input.GetMouseButtonDown(0))
             {
                 //setting a variable to ClickPosition to prevent recalling of the method
                 SelectedPosition = ClickPosition();
                 //if the selected position is not occupied by a game object
                 if (checkposition(SelectedPosition) == false)
                 {
                     //move the object to a position passing a parameter of SelectedPosition
                     Movement(SelectedPosition);
                 }
                 //else the movement does not occur, and a debug of a block is created
                 else
                     Debug.Log("Movement Blocked");
             }
         }
     }
 }
 void Movement(Vector3 Position)
 {
     //Sets the Selected object as the GameObject clicked on
     GameObject SelectedOb = SelectedItem;
     //Sets the Selected Objects Position to the Position clicked on
     SelectedOb.transform.position = Position;

 }
 public bool checkposition(Vector3 Position)
 {
     List<List<GameObject>> allActive = new List<List<GameObject>>();
     allActive.Add(GameObject.FindGameObjectsWithTag("Blocker").ToList());
     allActive.Add(GameObject.FindGameObjectsWithTag("PlayerHQ").ToList());
     allActive.Add(GameObject.FindGameObjectsWithTag("PlayerSoldierMarksman").ToList());
     allActive.Add(GameObject.FindGameObjectsWithTag("PlayerSoldierBasic").ToList());
     allActive.Add(GameObject.FindGameObjectsWithTag("PlayerSoldierArmored").ToList());
     allActive.Add(GameObject.FindGameObjectsWithTag("PlayerUnitRailgun").ToList());
     allActive.Add(GameObject.FindGameObjectsWithTag("EnemySoldierMarksman").ToList());
     allActive.Add(GameObject.FindGameObjectsWithTag("EnemySoldierBasic").ToList());
     allActive.Add(GameObject.FindGameObjectsWithTag("EnemySoldierArmored").ToList());
     allActive.Add(GameObject.FindGameObjectsWithTag("EnemyUnitRailgun").ToList());
     allActive.Add(GameObject.FindGameObjectsWithTag("EnemyHQ").ToList());
     
     if (allActive.Any(activeItem => activeItem.Any(activeGameObject => activeGameObject.transform.position == Position)))
     {
         return true;
     }
     return false;
 }

}

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 homanhquan · May 19, 2017 at 09:22 AM

My solution is: First need get GameObject to move, and Position clicked on, then you can using Vector3.MoveTowards to move your gameObject.

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

223 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 to limit palyer player movement to circle 0 Answers

Player does not move anymore when game is paused (Time.timeScale = 0) and resumed (Time.timeScale = 1) 0 Answers

Having trouble instantiating prefabs in my 2D game 1 Answer

what is the code to do somthing whan the user sliding the screen? 2 Answers

I need help with AI,Force not working 0 Answers


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