private void MoveSection() { if (SECTION_MOVELEFT) { Vector3 left = TrackDataHelper.SectionGetRotation(TRACK_DATA.SECTIONS[SECTION_CURRENT]) * -Vector3.right; TRACK_DATA.SECTIONS[SECTION_CURRENT].SECTION_POSITION += left * Section_MOVEAMOUNT; SECTION_MOVELEFT = false; } if (SECTION_MOVERIGHT) { Vector3 right = TrackDataHelper.SectionGetRotation(TRACK_DATA.SECTIONS[SECTION_CURRENT]) * Vector3.right; TRACK_DATA.SECTIONS[SECTION_CURRENT].SECTION_POSITION += right * Section_MOVEAMOUNT; SECTION_MOVERIGHT = false; } if (SECTION_MOVEFORWARD) { Vector3 forward = TrackDataHelper.SectionGetRotation(TRACK_DATA.SECTIONS[SECTION_CURRENT]) * Vector3.forward; TRACK_DATA.SECTIONS[SECTION_CURRENT].SECTION_POSITION += forward * Section_MOVEAMOUNT; SECTION_MOVEFORWARD = false; } if (SECTION_MOVEBACK) { Vector3 back = TrackDataHelper.SectionGetRotation(TRACK_DATA.SECTIONS[SECTION_CURRENT]) * Vector3.back; TRACK_DATA.SECTIONS[SECTION_CURRENT].SECTION_POSITION += back * Section_MOVEAMOUNT; SECTION_MOVEBACK = false; } }
private void UpdateTileColors() { if (!setTileColors) { return; } else { setTileColors = false; } Color32[] cols = MESH_TRACKFLOOR.sharedMesh.colors32; for (int i = 0; i < MESH_TRACKFLOOR.sharedMesh.triangles.Length; i += 3) { TrTile tile = TrackDataHelper.TileFromTriangleIndex(i / 3, E_TRACKMESH.FLOOR, TRACK_DATA); if (tile.TILE_TYPE == E_TILETYPE.BOOST) { Debug.Log("Found boost!"); cols[MESH_TRACKFLOOR.sharedMesh.triangles[i + 0]] = Color.blue; cols[MESH_TRACKFLOOR.sharedMesh.triangles[i + 1]] = Color.blue; cols[MESH_TRACKFLOOR.sharedMesh.triangles[i + 2]] = Color.blue; } } MESH_TRACKFLOOR.sharedMesh.colors32 = cols; }
private void UpdateShipLighting() { // get ship color from vertex colors of track mesh RaycastHit hit; Vector3 to = r.currentSection.SECTION_POSITION; to.y -= 5; if (Physics.Linecast(transform.position, to, out hit, 1 << LayerMask.NameToLayer("TrackFloor"))) { int tri = hit.triangleIndex; MeshCollider mc = hit.collider as MeshCollider; Mesh m = mc.sharedMesh; // get tile TrTile tile = TrackDataHelper.TileFromTriangleIndex(tri, E_TRACKMESH.FLOOR, RaceSettings.trackData.TRACK_DATA); if (tile.TILE_TYPE == E_TILETYPE.BOOST) { shipColor = Color.blue; } else if (tile.TILE_TYPE == E_TILETYPE.WEAPON) { shipColor = Color.red; } else { shipColor = Color.Lerp(shipColor, m.colors32[m.triangles[hit.triangleIndex * 3]], Time.deltaTime * 15); } } r.mesh.GetComponent <Renderer>().material.SetColor("_Color", shipColor); }
private void UpdateCurrentSection() { if (!iSectionFound) { return; } // update respawn respawnPosition = r.currentSection.SECTION_NEXT.SECTION_POSITION; respawnPosition.y += r.settings.AG_HOVER_HEIGHT; respawnRotation = TrackDataHelper.SectionGetRotation(r.currentSection.SECTION_NEXT); }
private void UpdateDirection() { Vector3 trackRot = TrackDataHelper.SectionGetRotation(r.currentSection) * Vector3.forward; float dot = Vector3.Dot(transform.forward, trackRot.normalized); if (dot > 0) { r.facingFoward = true; } else if (dot < 0) { r.facingFoward = false; } }
private void OnDrawGizmos() { if (TRACK_DATA == null) { return; } Vector3 pos = Vector3.zero; Vector3 pos2 = Vector3.zero; Color gizmoColor = Color.white; for (int i = 0; i < TRACK_DATA.SECTIONS.Count; i++) { // get positions to draw line pos = TRACK_DATA.SECTIONS[i].SECTION_POSITION; pos2 = pos + TRACK_DATA.SECTIONS[i].SECTION_NORMAL * 1; // draw sphere and line gizmoColor = Color.white; if ((TR_FLAGPAINTER != null && TRACK_DATA.SECTIONS[i] == highlightedSection && PAINT_MODE == E_PAINTMODE.SECTION) || (EDIT_SECITIONPOSITIONS && i == SECTION_CURRENT)) { gizmoColor = Color.red; } Gizmos.color = gizmoColor; Gizmos.DrawWireSphere(pos, 0.2f); Gizmos.DrawWireSphere(pos2, 0.2f); Gizmos.DrawLine(pos, pos2); // draw line between sections if (i < TRACK_DATA.SECTIONS.Count - 1) { pos2 = TRACK_DATA.SECTIONS[i + 1].SECTION_POSITION; Gizmos.DrawLine(pos, pos2); } // draw orientation pos = pos + TRACK_DATA.SECTIONS[i].SECTION_NORMAL * 0.5f; pos2 = pos + (TrackDataHelper.SectionGetRotation(TRACK_DATA.SECTIONS[i]) * Vector3.forward) * 1; Gizmos.DrawLine(pos, pos2); } }
private void UpdateInitialSection() { // try to find track section using a raycast RaycastHit hit; if (Physics.Raycast(transform.position, -transform.up, out hit, 1000.0f, 1 << LayerMask.NameToLayer("TrackFloor"))) { int tri = hit.triangleIndex; TrTile tile = TrackDataHelper.TileFromTriangleIndex(hit.triangleIndex, E_TRACKMESH.FLOOR, RaceSettings.trackData.TRACK_DATA); TrSection section = tile.TILE_SECTION; bool canUpdate = true; if (section.SECTION_TYPE == E_SECTIONTYPE.JUMP_START) { onJump = true; expectedLandSection = section.SECTION_NEXT; } if (section.SECTION_TYPE == E_SECTIONTYPE.JUMP_END) { onJump = false; if (section != expectedLandSection) { canUpdate = false; } } if (canUpdate) { r.initialSection = section; iSectionIndex = section.SECTION_INDEX; r.currentSection = r.initialSection; cSectionIndex = section.SECTION_INDEX; } iSectionFound = true; } else { iSectionFound = false; } }
public void FindSpawnTiles() { // clear any previous spawns spawnPositions.Clear(); spawnRotations.Clear(); // search through each tile for spawn tiles for (int i = 0; i < TRACK_DATA.TILES_FLOOR.Count; i++) { // if this tile is a spawn tile then add it to the spawn transform arrays if (TRACK_DATA.TILES_FLOOR[i].TILE_TYPE == E_TILETYPE.SPAWN) { Vector3 spawnPos = TRACK_DATA.TILES_FLOOR[i].TILE_POSITION; spawnPos.y += 0.5f; spawnPositions.Add(spawnPos); spawnRotations.Add(TrackDataHelper.SectionGetRotation(TRACK_DATA.TILES_FLOOR[i].TILE_SECTION)); Vector3 cameraPos = TRACK_DATA.TILES_FLOOR[i].TILE_SECTION.SECTION_POSITION; cameraPos.y += 0.5f; spawnCameraLocations.Add(cameraPos); } } }
private void FollowTrack() { // parent camera transform.parent = r.transform; // only follow track if current section exist if (r.currentSection == null) { return; } // position and rotate track helper (this is so we have a transform to work with) TrSection current = r.currentSection; Vector3 currentSegment = current.SECTION_POSITION; cameraHelper.transform.position = currentSegment; Quaternion currentRot = TrackDataHelper.SectionGetRotation(current); cameraHelper.transform.rotation = currentRot; // figure out the camera's offset to the track Vector3 cameraOffset = cameraHelper.transform.InverseTransformPoint(transform.position); // figure out which way the ship is facing and interpolate track direction dot product value to it Vector3 trackForward = cameraHelper.transform.forward; tcDirectionLag = Mathf.Lerp(tcDirectionLag, Vector3.Dot(transform.forward, trackForward), Time.deltaTime * (tcSpeed * 0.5f)); tcTrackOffset = Vector3.Lerp(tcTrackOffset, cameraOffset * tcDirectionLag, Time.deltaTime * (tcSpeed * 0.65f)); tcFinalOffset = Vector3.Lerp(tcFinalOffset, cameraOffset, Time.deltaTime * (tcSpeed * 0.65f)); // figure out which side of the track the camera is on (this is for positioning reasons) Vector3 trackSide = cameraHelper.transform.right; float sideDot = Vector3.Dot(transform.forward, trackSide); float sideFinal = Mathf.Sign(cameraOffset.x) * -Mathf.Sign(sideDot); // interpolate positions tcX = Mathf.Lerp(tcX, -tcTrackOffset.x * r.settings.CAMERA_OFFSET_SENSITIVITY.x, Time.deltaTime * (r.settings.CAMERA_OFFSET_SPEED.x)); tcY = Mathf.Lerp(tcY, Mathf.Abs(tcFinalOffset.x) * r.settings.CAMERA_OFFSET_SENSITIVITY.y, Time.deltaTime * (r.settings.CAMERA_OFFSET_SPEED.y * 0.15f)); tcZ = Mathf.Lerp(tcZ, (Mathf.Abs(cameraOffset.x) * r.settings.CAMERA_OFFSET_SENSITIVITY.z) * (sideFinal * (1 - Mathf.Abs(tcDirectionLag))), Time.deltaTime * (r.settings.CAMERA_OFFSET_SPEED.z * 0.4f)); // increase/decrease distance to ship on slopes float upDir = Vector3.Dot(Vector3.up, r.transform.forward); tcPitchOffset = Mathf.Lerp(tcPitchOffset, upDir * 0.2f, Time.deltaTime * tcSpeed); // fall Offsets if (r.sim.isShipGrounded) { tcFallTimer = 0; tcFallTimerGain = 0; } else { tcFallTimer += Time.deltaTime; } if (tcFallTimer > 0.2f) { tcFallTimerGain = Mathf.Lerp(tcFallTimerGain, 0.8f, Time.deltaTime); tcFallLagY = Mathf.Lerp(tcFallLagY, 0.4f, Time.deltaTime * tcFallTimerGain); tcFallLagZ = Mathf.Lerp(tcFallLagZ, -0.8f, Time.deltaTime * (tcFallTimerGain * 4)); } else { tcFallTimerGain = 0; tcFallLagY = Mathf.Lerp(tcFallLagY, 0.0f, Time.deltaTime * 4); tcFallLagZ = Mathf.Lerp(tcFallLagZ, 0.0f, Time.deltaTime * 4); } // apply camera offset transform.localPosition = new Vector3(r.settings.CAMERA_OFFSET_TRACK.x + tcX, r.settings.CAMERA_OFFSET_TRACK.y + tcY + tcFallLagY, r.settings.CAMERA_OFFSET_TRACK.z + tcZ + tcPitchOffset + tcFallLagZ); // update Rotation transform.rotation = r.transform.rotation; // update FoV r.cam.fieldOfView = 75.0f; }
void OnSceneGUI() { HandleUtility.Repaint(); // do not allow selection HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); // get target class TrFlagPainter thisTarget = (TrFlagPainter)target; // cycle tile types if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.KeypadPlus) { if (thisTarget.DATA_FE.PAINT_MODE == E_PAINTMODE.TILE) { switch (currentTileType) { case E_TILETYPE.FLOOR: currentTileType = E_TILETYPE.BOOST; break; case E_TILETYPE.BOOST: currentTileType = E_TILETYPE.WEAPON; break; case E_TILETYPE.WEAPON: currentTileType = E_TILETYPE.RECHARGE; break; case E_TILETYPE.RECHARGE: currentTileType = E_TILETYPE.SPAWN; break; case E_TILETYPE.SPAWN: currentTileType = E_TILETYPE.FLOOR; break; } } else if (thisTarget.DATA_FE.PAINT_MODE == E_PAINTMODE.SECTION) { switch (currentSectionType) { case E_SECTIONTYPE.NORMAL: currentSectionType = E_SECTIONTYPE.LOGIC_STARTLINE; break; case E_SECTIONTYPE.LOGIC_STARTLINE: currentSectionType = E_SECTIONTYPE.JUNCTION_START; break; case E_SECTIONTYPE.JUNCTION_START: currentSectionType = E_SECTIONTYPE.JUNCTION_END; break; case E_SECTIONTYPE.JUNCTION_END: currentSectionType = E_SECTIONTYPE.JUMP_START; break; case E_SECTIONTYPE.JUMP_START: currentSectionType = E_SECTIONTYPE.JUMP_END; break; case E_SECTIONTYPE.JUMP_END: currentSectionType = E_SECTIONTYPE.PITLANE_ENTRANCE; break; case E_SECTIONTYPE.PITLANE_ENTRANCE: currentSectionType = E_SECTIONTYPE.NORMAL; break; } } } // raycast from scene view mouse position RaycastHit hit; Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); Mesh m = thisTarget.DATA_FE.MESH_TRACKFLOOR.sharedMesh; GUIStyle label = new GUIStyle(); label.fontStyle = FontStyle.Bold; if (Physics.Raycast(ray, out hit, 1 << LayerMask.NameToLayer("TrackFloor"))) { TrTile tile = TrackDataHelper.TileFromTriangleIndex(hit.triangleIndex, E_TRACKMESH.FLOOR, thisTarget.TRACK_DATA); if (thisTarget.DATA_FE.PAINT_MODE == E_PAINTMODE.TILE) { // draw mesh Vector3[] verts = new Vector3[4]; int t = hit.triangleIndex * 3; verts[0] = hit.transform.TransformPoint(m.vertices[m.triangles[t + 0]]); verts[1] = hit.transform.TransformPoint(m.vertices[m.triangles[t + 1]]); verts[2] = hit.transform.TransformPoint(m.vertices[m.triangles[t + 2]]); verts[3] = hit.transform.TransformPoint(m.vertices[m.triangles[t + 0]]); // change mesh color depending on face type Color col = Color.grey; Color outline = col; switch (tile.TILE_TYPE) { case E_TILETYPE.BOOST: col = Color.blue; break; case E_TILETYPE.WEAPON: col = Color.red; break; case E_TILETYPE.SPAWN: col = Color.yellow; break; case E_TILETYPE.RECHARGE: col = Color.green; break; } switch (currentTileType) { case E_TILETYPE.BOOST: outline = Color.blue; break; case E_TILETYPE.WEAPON: outline = Color.red; break; case E_TILETYPE.SPAWN: outline = Color.yellow; break; case E_TILETYPE.RECHARGE: outline = Color.green; break; } outline *= 0.8f; col.a = 0.2f; Handles.DrawSolidRectangleWithOutline(verts, col, outline); // draw label string flip = tile.TILE_SECOND ? "Flipped" : "Normal"; Vector3 pos = tile.TILE_POSITION; col.a = 1.0f; label.normal.textColor = col; Handles.Label(pos, "Current = " + tile.TILE_TYPE.ToString() + "_" + flip, label); pos += tile.TILE_SECTION.SECTION_NORMAL * 0.4f; outline.a = 1.0f; label.normal.textColor = outline; Handles.Label(pos, "Paint = " + currentTileType.ToString() + "_" + flip, label); if (Event.current.type == EventType.mouseUp && Event.current.button == 0) { // apply change tile.TILE_TYPE = currentTileType; // cache change thisTarget.DATA_FE.CACHE_TILES[tile.TILE_INDEX] = currentTileType; } else if (Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.R) { // apply change tile.TILE_TYPE = E_TILETYPE.FLOOR; // cache change thisTarget.DATA_FE.CACHE_TILES[tile.TILE_INDEX] = currentTileType; } } else if (thisTarget.DATA_FE.PAINT_MODE == E_PAINTMODE.SECTION) { thisTarget.DATA_FE.highlightedSection = tile.TILE_SECTION; // change mesh color depending on section type Color col = Color.grey; Color outline = col; switch (tile.TILE_SECTION.SECTION_TYPE) { case E_SECTIONTYPE.JUMP_START: col = Color.green; break; case E_SECTIONTYPE.JUMP_END: col = Color.green; col *= 0.5f; break; case E_SECTIONTYPE.JUNCTION_START: col = Color.red; break; case E_SECTIONTYPE.JUNCTION_END: col = Color.red; col *= 0.5f; break; case E_SECTIONTYPE.LOGIC_STARTLINE: col = Color.blue; break; case E_SECTIONTYPE.PITLANE_ENTRANCE: col = Color.yellow; break; } switch (currentSectionType) { case E_SECTIONTYPE.JUMP_START: outline = Color.green; break; case E_SECTIONTYPE.JUMP_END: outline = Color.green; outline *= 0.5f; break; case E_SECTIONTYPE.JUNCTION_START: outline = Color.red; break; case E_SECTIONTYPE.JUNCTION_END: outline = Color.red; outline *= 0.5f; break; case E_SECTIONTYPE.LOGIC_STARTLINE: outline = Color.blue; break; case E_SECTIONTYPE.PITLANE_ENTRANCE: outline = Color.yellow; break; } outline *= 0.8f; outline.a = 1.0f; col.a = 1.0f; Vector3 pos = tile.TILE_SECTION.SECTION_POSITION; label.normal.textColor = col; Handles.Label(pos, "Current = " + tile.TILE_SECTION.SECTION_TYPE.ToString(), label); pos += tile.TILE_SECTION.SECTION_NORMAL * 0.4f; label.normal.textColor = outline; Handles.Label(pos, "Paint = " + currentSectionType.ToString(), label); if (Event.current.type == EventType.mouseUp && Event.current.button == 0) { // apply change tile.TILE_SECTION.SECTION_TYPE = currentSectionType; // cache change thisTarget.DATA_FE.CACHE_SECTIONS[tile.TILE_SECTION.SECTION_INDEX] = currentSectionType; } } } }