- Home /
The question is answered, right answer was accepted
Finding Edges
Hello there, i want know how to find edge for an object manually using script(C#). Basically i have created board using script, here i need know the edges for the Board.
Camera position is: Transform.position.x = 5; Transform.position.y = 13; Transform.position.z = -7.5;
Camera rotation is: Transform.rotation.x = 45;
Here is the coding...
using UnityEngine; using System.Collections;
[ExecuteInEditMode] [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] [RequireComponent(typeof(MeshCollider))]
public class Board : MonoBehaviour
{
// Board Creation Variables/....
public static int size_x=10;
public static int size_z=10;
public static float tileSize=1.0f;
// Use this for initialization
void Start ()
{
CreateBoard();
}
// Board Creation/.....
public void CreateBoard()
{
int numTiles = size_x * size_z;
int numTris = numTiles * 2;
int vsize_x = size_x + 1;
int vsize_z = size_z + 1;
int numVerts = vsize_x * vsize_z;
Vector3[] vertices = new Vector3[numVerts];
Vector3[] normals = new Vector3[numVerts];
Vector2[] uv = new Vector2[numVerts];
int[] triangles = new int[numTris*3];
int x,z;
for(z=0; z < vsize_z; z++)
{
for(x=0 ; x < vsize_x; x++)
{
vertices[ z * vsize_x + x]=new Vector3(x * tileSize, 0 , z*tileSize);
normals[z * vsize_x + x]=Vector3.up;
uv[z * vsize_x + x]=new Vector2((float)x / size_x,(float)z / size_z);
}
}
for(z=0; z < size_z; z++)
{
for(x=0 ;x < size_x; x++)
{
int squareIndex = z * size_x + x;
int trioffset = squareIndex * 6;
triangles[trioffset + 0] = z * vsize_x + x + 0;
triangles[trioffset + 2] = z * vsize_x + x + vsize_x + 1;
triangles[trioffset + 1] = z * vsize_x + x + vsize_x + 0;
triangles[trioffset + 3] = z * vsize_x + x + 0;
triangles[trioffset + 5] = z * vsize_x + x + 1;
triangles[trioffset + 4] = z * vsize_x + x + vsize_x + 1;
}
}
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uv;
MeshFilter mesh_filter = GetComponent<MeshFilter>();
MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
MeshCollider mesh_collider = GetComponent<MeshCollider>();
mesh_collider.sharedMesh = mesh;
mesh_filter.mesh = mesh;
}
}
i also attached my Texture image. Here is my problem my character moving towards the number 1,2,3,4..., if he reach number 10 in the board he need to rotate himself to 90 degree up. So for that i need to know how to calculate the edge of the border without using triggers.
any help will be appreciated
-Prasanna.
Answer by Berenger · Mar 20, 2014 at 09:10 AM
Does your character knows what tile it's on ? Because you could calculate if the tile on the edge or not with something like :
if( currentTile % size_x == 0 || (currentTile-1) % size_x == 0
|| currentTile < size_x || currentTile > size_x * (size_x-1) )
...
No my character don't know, but i used something like this:
using UnityEngine; using System.Collections;
public class Character$$anonymous$$ovement : $$anonymous$$onoBehaviour
{ // Player $$anonymous$$ovement Variables/....
public static int $$anonymous$$yPosition, NextPositionValue;
public static float $$anonymous$$oveSpeed = 0.5f, CurrentTile = 0.5f, NextTile, EdgeTile, StartTile, EdgePositionValue, Edge;
public static bool Condition = false, Clicked = false;
public static Vector3 StartPosition, CurrentPosition, PreviousPosition, NextPosition, NewPosition, EdgePosition;
public void Border()
{
// Create a Border for 10th value...
}
public void Update()
{
if(Clicked == true)
{
Edge = 3.5f;
StartPosition = this.transform.position;
CurrentPosition = StartPosition;
NextTile = CurrentTile + Dice.value;
EdgeTile = CurrentTile + Edge;
$$anonymous$$yPosition = (int)this.transform.position.x;
NextPositionValue = $$anonymous$$yPosition + Dice.value;
NextPosition = CurrentPosition + new Vector3(Dice.value,0,0);
EdgePosition = CurrentPosition + new Vector3(Edge, 0, 0);
Clicked = false;
}
if(CurrentPosition.x < NextPosition.x)
{
if(Condition == true)
{
animation.Play("Walk");
transform.Translate(Vector3.forward * $$anonymous$$oveSpeed * Time.deltaTime);
}
}
if(this.transform.position.x >= NextPosition.x)
{
Condition = false;
animation.Play("Idle");
}
if(this.transform.position.x == EdgePosition.x)
{
Debug.Log("Enter");
}
if(Ladder15.trigger == true)
{
transform.Rotate (new Vector3 (0, 90, 0) * Time.deltaTime);
}
}
}
this is for character movement. Here i used to find the edge, but i can't.
If you start at 1, you can know the index of the tile. A dice value positive means +1, negative means -1.
By the way, you should take a look at iTween for the translation, that would probably make it cooler ;)
I tried iTween for rotation using trigger, my case the trigger won't work. In my Character$$anonymous$$ovement script i have added the edge variables, i declare the edge is 3.5f; but it doesn't work. Where can i find the edge, in Chareacter$$anonymous$$ovement script or Board script.
So the basic thing what i want to know if my character stand the edge(10th Box). Character need to turn himself to 90 degree up, i already added my scripts for Board and Character$$anonymous$$ovement. What i did in Character $$anonymous$$ovement script i try ti find the edge, but i can't get that.
-Prasanna
Follow this Question
Related Questions
Rotating Cuboid Around Pivot 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Rotate More than Two GameObjects 1 Answer
A node in a childnode? 1 Answer