// This should only be done if RailIsConnected check returns true void TransferToNextRail(bool atRailExit) { Tile railTile = currentRail.GetComponent <Tile>(); // Find the adjacent tile in the specified direction int connectionDirection = atRailExit ? currentRail.exitDirection : currentRail.entryDirection; connectionDirection += railTile.direction; connectionDirection %= 4; Vector2Int adjacentTileCoords = railTile.tileCoords + DirectionToVector(connectionDirection); Tile adjacentTile = tileManager.GetTile(adjacentTileCoords); // Transfer to the connecting rail Rail[] adjacentRails = adjacentTile.GetComponents <Rail>(); int adjacentConnectionDirection = (connectionDirection + 2 - adjacentTile.direction + 4) % 4; foreach (Rail adjacentRail in adjacentRails) { if (adjacentRail.entryDirection == adjacentConnectionDirection || adjacentRail.exitDirection == adjacentConnectionDirection) { bool atAdjacentRailExit = (adjacentConnectionDirection == adjacentRail.exitDirection); float overshoot = atRailExit ? railDistance - currentRail.trackLength : -railDistance; currentRail = adjacentRail; railDistance = atAdjacentRailExit ? adjacentRail.trackLength - overshoot : overshoot; if (atRailExit == atAdjacentRailExit) { alignedWithRail = !alignedWithRail; } } } }
bool RailIsConnected(Rail rail, bool atRailExit) { Tile railTile = rail.GetComponent <Tile>(); // Find the adjacent tile in the specified direction int connectionDirection = atRailExit ? rail.exitDirection : rail.entryDirection; connectionDirection += railTile.direction; connectionDirection %= 4; Vector2Int adjacentTileCoords = railTile.tileCoords + DirectionToVector(connectionDirection); if (!tileManager.IsWithinArray(adjacentTileCoords)) { return(false); } Tile adjacentTile = tileManager.GetTile(adjacentTileCoords); // Check if it has a connecting rail Rail[] adjacentRails = adjacentTile.GetComponents <Rail>(); int adjacentConnectionDirection = (connectionDirection + 2 - adjacentTile.direction + 4) % 4; foreach (Rail adjacentRail in adjacentRails) { if (adjacentRail.entryDirection == adjacentConnectionDirection || adjacentRail.exitDirection == adjacentConnectionDirection) { return(true); } } return(false); }
/// <summary>惑星fromからtoへのパスを設定. カメラ設定用にPlayerも必要</summary> public void Initialize(Transform from, Transform to, Transform player) { // コンポーネントを取得. Rail = this.GetComponent <Rail>(); m_path = Rail.GetComponent <CinemachineSmoothPath>(); // パスの設定. SetWayPoints(from, to, out Vector3 start); transform.SetPositionAndRotation(start, Quaternion.identity); m_path.InvalidateDistanceCache(); Rail.PathDuration = Mathf.Max(m_path.PathLength / m_moveSpeed, m_minimumDuration); // 目標の設定. Rail.GoalPlanet = to; // カメラの設定. SetCameraParameters(player); }
private void UpdateCollisionMesh( Rail rail ) { MeshCollider mc = rail.GetComponent<Collider>() as MeshCollider; List<Vector3> verts = new List<Vector3>(); List<int> tris = new List<int>(); for( int i = 0; i < rail.nodes.Count; i++ ) { Vector3 lookDir; if( i <= 0 ) { lookDir = rail.nodes[1] - rail.nodes[0]; } else if( i > 0 && i < rail.nodes.Count - 1 ) { lookDir = ( rail.nodes[i + 1] - rail.nodes[i] ).normalized + ( rail.nodes[i] - rail.nodes[i - 1] ).normalized / 2f; } else { lookDir = rail.nodes[rail.nodes.Count - 1] - rail.nodes[rail.nodes.Count - 2]; } Quaternion lootRot = Quaternion.LookRotation( lookDir ); //Create vert triangle verts.Add( new Vector3( -0.1f, 0.05f, 0f ) ); verts.Add( new Vector3( 0.1f, 0.05f, 0f ) ); verts.Add( new Vector3( 0f, -0.05f, 0f ) ); //Move vert triangle to node for( int j = verts.Count - 3; j < verts.Count; j++ ) { verts[j] = verts[j] + rail.nodes[i]; } //Point first triangle to next node for( int j = verts.Count - 3; j < verts.Count; j++ ) { Vector3 vertDir = verts[j] - rail.nodes[i]; vertDir = lootRot * vertDir; verts[j] = vertDir + rail.nodes[i]; } } //Build the triangles //Add first node end triangle tris.Add( 0 ); tris.Add( 1 ); tris.Add( 2 ); for( int i = 0; i < rail.nodes.Count - 1; i++ ) { //Top tris.Add( 3 * i + 3 ); tris.Add( 3 * i + 1 ); tris.Add( 3 * i + 0 ); tris.Add( 3 * i + 4 ); tris.Add( 3 * i + 1 ); tris.Add( 3 * i + 3 ); //Right tris.Add( 3 * i + 4 ); tris.Add( 3 * i + 5 ); tris.Add( 3 * i + 1 ); tris.Add( 3 * i + 1 ); tris.Add( 3 * i + 5 ); tris.Add( 3 * i + 2 ); //Left tris.Add( 3 * i + 3 ); tris.Add( 3 * i + 0 ); tris.Add( 3 * i + 5 ); tris.Add( 3 * i + 5 ); tris.Add( 3 * i + 0 ); tris.Add( 3 * i + 2 ); } //Add last node end triangle tris.Add( verts.Count - 1 ); tris.Add( verts.Count - 2 ); tris.Add( verts.Count - 3 ); Mesh mesh = new Mesh(); mesh.vertices = verts.ToArray(); mesh.triangles = tris.ToArray(); mc.sharedMesh = mesh; }