Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 aybeeomari_ · Jan 27, 2015 at 08:13 PM · c#rotationmovementrotation detection

Grid-based game movement

I'm trying to make a game in which you direct an object towards a goal by creating a path. This path will be created on a grid by clicking adjacent tiles and toggling through left, right and forward in order to get the object to it's destination.

Right now, it looks like this:

alt text

I've figured out translating the player from one point to another on the grid by clicking on the tiles.

However, I still need to figure out

1) How to limit grid movement so that only adjacent tiles can be selected.

2) How to toggle between textures on each tile by clicking multiple times in order to determine what direction the object faces. Say I click once on a tile, it should be a forward facing arrow, click again, it should be an arrow pointing to the right.

This is the code for the GameManager script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GameManager : MonoBehaviour {
     public static GameManager instance;
     
     public GameObject TilePrefab;
     public GameObject UserPlayerPrefab;
     public GameObject AIPlayerPrefab;
     
     public int mapSize = 0;
     
     List <List<Tile>> map = new List<List<Tile>>();
     List <Player> players = new List<Player>();
     int currentPlayerIndex = 0;
     
     void Awake() {
         instance = this;
     }
     
     // Use this for initialization
     void Start () {        
         generateMap();
         generatePlayers();
     }
     
     // Update is called once per frame
     void Update () {
         
         players[currentPlayerIndex].TurnUpdate();
     }
     
     public void nextTurn() {
         if (currentPlayerIndex + 1 < players.Count) {
             currentPlayerIndex++;
         } else {
             currentPlayerIndex = 0;
         }
     }
     
     public void moveCurrentPlayer(Tile destTile) {
         players[currentPlayerIndex].moveDestination = destTile.transform.position + 1.5f * Vector3.up;
     }
     
     void generateMap() {
         map = new List<List<Tile>>();
         for (int i = 0; i < mapSize; i++) {
             List <Tile> row = new List<Tile>();
             for (int j = 0; j < mapSize; j++) {
                 Tile tile = ((GameObject)Instantiate(TilePrefab, new Vector3(i - Mathf.Floor(mapSize/2),0, -j + Mathf.Floor(mapSize/2)), Quaternion.Euler(new Vector3()))).GetComponent<Tile>();
                 tile.gridPosition = new Vector2(i, j);
                 row.Add (tile);
             }
             map.Add(row);
         }
     }
     
     void generatePlayers() {
         UserPlayer player;
         
         player = ((GameObject)Instantiate(UserPlayerPrefab, new Vector3(0 - Mathf.Floor(mapSize/2),1.5f, -0 + Mathf.Floor(mapSize/2)), Quaternion.Euler(new Vector3()))).GetComponent<UserPlayer>();
         
         players.Add(player);
         
 
         AIPlayer aiplayer = ((GameObject)Instantiate(AIPlayerPrefab, new Vector3(6 - Mathf.Floor(mapSize/2),1.5f, -4 + Mathf.Floor(mapSize/2)), Quaternion.Euler(new Vector3()))).GetComponent<AIPlayer>();
         
         players.Add(aiplayer);
     }
 }
 


This is the code for the tile pieces:

 using UnityEngine;
 using System.Collections;
 
 public class Tile : MonoBehaviour {
     
     public Vector2 gridPosition = Vector2.zero;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void OnMouseEnter() {
         transform.renderer.material.color = Color.blue;
         
         Debug.Log("my position is (" + gridPosition.x + "," + gridPosition.y);
     }
     
     void OnMouseExit() {
         transform.renderer.material.color = Color.white;
     }
     
     
     void OnMouseDown() {
     
         GameManager.instance.moveCurrentPlayer(this);
     }
     
 }

And this is the code for the player piece:

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
     
     public Vector3 moveDestination;
     public float moveSpeed = 10.0f;
     
     void Awake () {
         moveDestination = transform.position;
     }
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     public virtual void TurnUpdate () {
         
     }
 }
 


screen-shot-2015-01-27-at-110451-am.png (61.6 kB)
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 Noah Dyer · Jan 27, 2015 at 08:49 PM

To question 1, in addition to keeping track of the destination tile, you need to keep track of the current tile. Then you can check destinationTile against currentTile in a function called IsValidMove() or somesuch.

To question 2, similarly, you need to keep track of current orientation. I'd probably do it in an enum, but you could also do it with strings, etc. Basically, whenever the mouse is clicked, you check the current orientation, and change it to whatever you want to come next.

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

Making a bubble level (not a game but work tool) 1 Answer

Flip over an object (smooth transition) 3 Answers

Rotating Character 1 Answer

Orbit Camera Around Player And Add Force 3 Answers

Rotate towards movement direction 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