2 Replies - 5950 Views - Last Post: 29 September 2013 - 09:11 AM Rate Topic: -----

#1 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 411
  • Joined: 08-January 12

Unity C# object movement with grid.

Posted 20 September 2013 - 10:23 AM

Okay I have some screen shots here to help explain what I am trying to accomplish. The idea is that the player will click the game object and a grid will appear showing the path that piece can move much like chess. I would also like to use this same cube concept to create a fog of war which I pretty much already accomplished. I now have to try to make this same grid technique work specifically for each piece. For example a tank can move three spaces in any direction. Also obviously I don't want the cubes to spawn where there is a player or enemy object such as a tank or plane. I was trying to detect collision between the two objects in order shut down the active status of the child grid block.

Game object movement script
using UnityEngine;
using System.Collections;

public class PieceMovement : MonoBehaviour 
{
	public Terrain terrain;
	public GameObject obj;
	public TerrainGrid grid;
	private bool toggle = false;
	Vector3 pos;
	float y;

	
	private void onmousedown()
	{
		obj = GameObject.Find("Bomber");
		//lastObject = Bomber;
		pos = obj.transform.position;
		//Set the camera to the new position
		//Camera.mainCamera.transform.position = new Vector3(pos.x, y + 2f, pos.z);
		y = pos.y;
		if (toggle == true)
		{
 			grid.ShowGrid = true;
			toggle = false;
		}
		else
		{
			grid.ShowGrid = false;
			toggle = true;
		}
			
		//move the Bomber to the cliced position
		//Bomber.transform.position = grid.GetGridPosition(new Vector3 (x + 1f, y, z + 2f));
		Debug.Log ("Mouse click inside Piece script");
	} 
}






Grid Script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TerrainGrid : MonoBehaviour 
{
	//terrain grid is attached to
	public Terrain terrain; 
	public bool ShowGrid = false;
	public int CellSize = 1;
	
	GameObject go;
	
	private Vector3 pos;
	
	Rigidbody rb;
	
	private Vector3 terrainSize;
	private Vector3 origin;
	
	private int length;
	private int width;

	List<GameObject> objects;

	void Start ()
	{

		terrainSize = terrain.terrainData.size;
		origin = terrain.transform.position;
		
		length = (int)terrainSize.z / CellSize;
		width = (int)terrainSize.x / CellSize;
		
		objects = new List<GameObject>();
		
		BuildGrid();
	}
	
	void Update ()
	{
		foreach(GameObject obj in objects)
		{
			obj.SetActive(ShowGrid);
		}	
	}
	
	void BuildGrid()
	{

		for(int x = 0; x < length; x++)
		{
			for(int y = 0; y < width; y++)
			{
				GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
				pos = GetWorldPosition(new Vector2(y,x));
				pos += new Vector3(CellSize / 2f, terrain.SampleHeight(GetWorldPosition(new Vector2(y,x))), CellSize /2f);
				pos += new Vector3(0f,0.2f,0f);
				//set the position of the child block
				go.transform.position = pos;
				//Make the cubes red
				go.renderer.material.color = Color.black;
				go.name = x + "" + y;
				//add the rigidBody to the children
				rb = go.AddComponent<Rigidbody>();
				rb.mass = 10f;
				rb.drag = 1f;
				//rb.useGravity = false;
				//if(y==0) go a
					//go.renderer.material.color = Color.green;				
				
				
				
				go.transform.localScale = new Vector3(CellSize /2f, CellSize /2f, CellSize/2f);
				go.transform.parent = transform;
				//name = go.transform.parent.name;
				go.SetActive(false);
				objects.Add(go);
			}
		}
	}
	
	public Vector3 GetWorldPosition(Vector2 gridPosition)
	{
		return new Vector3(origin.z + (gridPosition.x * CellSize), origin.y, origin.x + (gridPosition.y * CellSize));
	}
	
	public Vector2 GetGridPosition(Vector3 worldPosition)
	{
		return new Vector2(worldPosition.z / CellSize, worldPosition.x / CellSize);
	}
	
	void onmousedown()
	{
		//transform.position = GetGridPosition(new Vector3 (1f, 1f, 2f));
		Debug.Log ("From the TerrainGrid");
	}
	
	void OnCollisionmove(Collision theCollision)
	{
		
	}
}



Shot 1

Shot 2

Shot 3

This post has been edited by modi123_1: 20 September 2013 - 10:59 AM
Reason for edit:: fixed img issue


Is This A Good Question/Topic? 0
  • +

Replies To: Unity C# object movement with grid.

#2 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 411
  • Joined: 08-January 12

Re: Unity C# object movement with grid.

Posted 20 September 2013 - 10:53 AM

Updated**
Was This Post Helpful? 0
  • +
  • -

#3 AdoTheLimey  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 99
  • Joined: 28-June 10

Re: Unity C# object movement with grid.

Posted 29 September 2013 - 09:11 AM

your mouse down function will not get called when the mouse button is pressed, its case sensitive. It needs to be onmousedown()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1