//handle mouse input by casting a ray from the cursor through the camera at the mesh collider and returning the index of the nearest vertex int HandleMouseInput() { RaycastHit hit; mc.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity); cursorPoint = hit.point; // used for the green gizmo, see under Gizmos Vector3 gp = grid.WorldToGrid(hit.point); // the point that was hit in grid coordinates return(RowColumn2Index(Mathf.RoundToInt(-gp.z) + 1, Mathf.RoundToInt(gp.x) + 1)); // remember, the index is related to the grid coordinates }
//Lukker Update /** Finder ud af om man må bevæge sig i den givne retning */ Vector3 FindNextFace() { //we will be operating in grid space, so convert the position Vector3 newPosition = grid.WorldToGrid(transform.localPosition); //Add one grid unit onto position in the picked direction BevaegeRetning(ref newPosition); //return the position in world space return(grid.GridToWorld(newPosition)); }
Vector3 FindNextFace() { //we will be operating in grid space, so convert the position Vector3 newPosition = grid.WorldToGrid(transform.position); //Retninger Vector3 opVec = new Vector3(270, 0, 0); Vector3 nedVec = new Vector3(90, 180, 0); Vector3 hoejreVec = new Vector3(0, 90, 270); Vector3 venstreVec = new Vector3(0, 270, 90); //first let's pick a random number for one of the four possible directions int i = Random.Range(0, 4); //now add one grid unit onto position in the picked direction if (i == 0) { newPosition = newPosition + new Vector3(1, 0, 0); rotation = hoejreVec; } else if (i == 1) { newPosition = newPosition + new Vector3(-1, 0, 0); rotation = venstreVec; } else if (i == 2) { newPosition = newPosition + new Vector3(0, 1, 0); rotation = opVec; } else if (i == 3) { newPosition = newPosition + new Vector3(0, -1, 0); rotation = nedVec; } ani.SetBool("Run", true); transform.rotation = Quaternion.Euler(rotation); //if we would wander off beyond the size of the grid turn the other way around /*for (int j = 0; j < 2; j++) { * if (Mathf.Abs (newPosition [j]) > grid.size [j]) * newPosition [j] -= Mathf.Sign (newPosition [j]) * 2.0f; * }*/ //return the position in world space return(grid.GridToWorld(newPosition)); }
Vector3 FindNextFace() { //we will be operating in grid space, so convert the position Vector3 newPosition = grid.WorldToGrid(cachedTransform.position); //first let's pick a random number for one of the four possible directions int i = Random.Range(0, 4); //now add one grid unit onto position in the picked direction if (i == 0) { newPosition = newPosition + new Vector3(1, 0, 0); } else if (i == 1) { newPosition = newPosition + new Vector3(-1, 0, 0); } else if (i == 2) { newPosition = newPosition + new Vector3(0, 1, 0); } else if (i == 3) { newPosition = newPosition + new Vector3(0, -1, 0); } //if we would wander off beyond the size of the grid turn the other way around for (int j = 0; j < 2; j++) { if (Mathf.Abs(newPosition[j]) > grid.size[j] / grid.spacing[j]) { newPosition[j] -= Mathf.Sign(newPosition[j]) * 2.0f; } } //return the position in world space return(grid.GridToWorld(newPosition)); }
/// <summary>Move the snake into a direction</summary> private void Move(Vector3 dir) { // first check if the destination we want to move towards is within bounds; if not we print a message if (segments.Count > 0) // if the snake has no head we can't do any check, so make sure there is at least one segment { if (OutsideRange(grid.WorldToGrid(segments[0].position + dir))) { Debug.Log("Illegal Move, turn around!"); return; } } // now let's grow our snake; we grow only one segment per turn if (grow > 0) { Vector3 tailPos = segments.Count > 0 ? segments.Last().position : transform.position; // if the list is empty (no head) we spawn our head where the snake object lies var newSegment = Instantiate(segmentPrefab) as Transform; newSegment.parent = transform; // this isn't really needed, but it keeps things organized newSegment.position = tailPos; // place the new segment at the tail segments.Add(newSegment); // and append it to the list --grow; // decrement the growth counter } movable = false; // we are ready to move, so stop all movment input // from behind move every segment to the position of its predecessor; note that the animation is on hold until every movement has been assigned for (int i = segments.Count - 1; i > 0; --i) { StartCoroutine(MoveWithSpeed(segments[i], segments[i - 1].position)); } // finally, move the head StartCoroutine(MoveWithSpeed(segments[0], segments[0].position + dir)); // all movement has been issued, now we can release the segments onHold = false; }
public Vector3 WorldToGrid(Vector3 world) { return(primary.WorldToGrid(world)); }