public void RespawnPlayer() { if (respawnPoint.GetNodeCount() <= 1) { return; } float minDistance = float.MaxValue; int minDistanceNode = 0; int count = 0; for (int index = 0; index < respawnPoint.GetNodeCount(); index++) { if (Vector3.Distance(transform.position, respawnPoint.GetNode(index)) < minDistance) { minDistance = Vector3.Distance(transform.position, respawnPoint.GetNode(index)); minDistanceNode = count; } count++; } if (minDistanceNode >= respawnPoint.GetNodeCount()) { minDistanceNode = 0; } GetComponent <Rigidbody> ().velocity = Vector3.zero; GetComponent <Rigidbody> ().angularVelocity = Vector3.zero; transform.position = respawnPoint.GetNode(minDistanceNode); transform.forward = respawnPoint.GetNode(minDistanceNode + 1) - transform.position; }
void LateUpdate() { minDistanceNode = FindNearestNode(trackNode, transform); if (Vector3.Distance(trackNode.GetNode(minDistanceNode), transform.position) > verticalRespawnPoint) { CallOnRespawn(); } }
void DebugDraw(SceneView sceneView) { TrackNodeTool trackNodeToolScript = (TrackNodeTool)target; TrackNode trackNode = trackNodeToolScript.trackNode; if (trackNode.GetNodeCount() <= 0) { return; } if (trackNode != null) { Transform trackNodeToolTransform = null; try { trackNodeToolTransform = trackNodeToolScript.GetComponent <Transform>(); } catch { doDrawTrajectory = false; while (subscribeCount > 0) { SceneView.onSceneGUIDelegate -= DebugDraw; subscribeCount--; } return; } for (int index = 0; index < trackNode.GetNodeCount(); index++) { if (trackNode.isLoopOpen && index == 0) { continue; } if ((index & 1) == 1) { Handles.color = Color.red; } else { Handles.color = Color.cyan; } Handles.DrawLine(trackNode.GetNode(index), trackNode.GetNode(index - 1)); } Handles.color = Color.blue; Handles.DrawLine(trackNode.GetNode(trackNode.GetNodeCount() - 1), trackNodeToolTransform.position); Handles.color = Color.yellow; Handles.DrawLine(trackNode.GetNode(0), trackNodeToolTransform.position); Handles.color = Color.green; Handles.DrawLine(trackNode.GetNode(cycleNodeIndex), trackNodeToolTransform.position); } }
private IEnumerator CheckIfRespawnIsNeeded() { int node = TrackNode.FindNearestNode(trackNode, transform); if (Vector3.Distance(trackNode.GetNode(node), transform.position) > respawnDistance) { respawnNode = node; CallRespawnAction(); } yield return(new WaitForSeconds(0.5f)); blockSpawnCheck = false; }
private void Update() { timeAlive += Time.deltaTime * rb.velocity.magnitude; lastVelocitySqr = velocitySqr; velocitySqr = Vector3.Dot(rb.velocity, transform.forward) * Mathf.Abs(Vector3.Dot(rb.velocity, transform.forward)); SetRotationUp(); UpdateWaypointID(); nextWaypoint = GetNextWayPoint(lookAhead); Vector3 trackDirection = (trackNode.GetNode(waypointNodeID + 1) - trackNode.GetNode(waypointNodeID)).normalized; if (Vector3.Dot(trackDirection, transform.forward) < 0) { respawn.CallRespawnAction(); } Vector3 nrml = Vector3.zero; GetNormal(out nrml); if (!IdleMode && isGrounded) { pid.RunPID(); motorTorque = Mathf.Lerp(motorTorque, pid.controlVariable, 0.5f); if (motorTorque >= 0) { Go(motorTorque); } else { Stop(0.1f); } } SetSteeringAngle(); ApplyVelocityDrag(velocityDrag); if (trackNode.isLoopOpen && ((waypointNodeID + 1) >= (trackNode.GetNodeCount()))) { IdleMode = true; } }
private float SetSteeringAngle() { nearestNode = Respawn.FindNearestNode(trackNode, transform); float farNodeWeightHolder = 1; Vector3 targetDirection = Vector3.zero; Debug.DrawRay(frontWheel.transform.position, frontWheel.transform.forward * 2, Color.blue); int numberOfNodes = aiSettings.numberNodeInPrediction; for (int j = 0; j < aiSettings.numberNodeInPrediction; j++) { Vector3 nextNode = trackNode.GetNode(nearestNode + j); Vector3 nextDirection = (nextNode - frontWheel.transform.position).normalized; nextDirection -= Vector3.Dot(nextDirection, frontWheel.transform.up) * frontWheel.transform.up; if (Vector3.Dot(nextDirection, frontWheel.transform.forward) > 0.25) { targetDirection += nextDirection * farNodeWeightHolder; if (j == 0) { Debug.DrawRay(frontWheel.transform.position, nextDirection, Color.red); } else { Debug.DrawRay(frontWheel.transform.position, nextDirection); } } else { numberOfNodes++; } farNodeWeightHolder *= aiSettings.farNodeWeight; } targetDirection = new Vector3(targetDirection.x, 0, targetDirection.z).normalized; float angle = Vector3.Angle(targetDirection, frontWheel.transform.forward); if (Vector3.Dot(targetDirection, frontWheel.transform.right) < 0) { angle = -angle; } float resultAngle = Mathf.Lerp(frontWheel.steerAngle, angle, aiSettings.steeringLerpTime * Time.deltaTime); if (resultAngle > 45) { resultAngle = 45; } else if (resultAngle < -45) { resultAngle = -45; } return(resultAngle); }
static public int FindNearestNode(TrackNode respawnPoint, Transform objToRespawn) { float minDistance = float.MaxValue; int minDistanceNode = 0; int count = 0; for (int index = 0; index < respawnPoint.GetNodeCount(); index++) { if (Vector3.Distance(objToRespawn.transform.position, respawnPoint.GetNode(index)) < minDistance) { minDistance = Vector3.Distance(objToRespawn.transform.position, respawnPoint.GetNode(index)); minDistanceNode = count; } count++; } return(minDistanceNode); }
void SetHandleAtIndex(int index) { TrackNodeTool trackNodeScript = (TrackNodeTool)target; TrackNode trackNode = trackNodeScript.trackNode; if (trackNode != null) { trackNodeScript.GetComponent <Transform>().position = trackNode.GetNode(index); } }
void SpawnNPC() { for (int i = 0; i < NPCList.Count; i++) { if (NPCList [i].activeSelf == false) { NPCList [i].SetActive(true); int node = 0; if (!trackNodes.isLoopOpen) { node = Random.Range(0, trackNodes.GetNodeCount() - 1); } NPCList[i].transform.position = trackNodes.GetNode(node); NPCList [i].transform.forward = trackNodes.GetNode(node + 1) - trackNodes.GetNode(node); return; } } }
static public int FindNearestNode(TrackNode respawnPoint, Transform objToRespawn) { float minDistance = float.MaxValue; int minDistanceNode = 0; for (int index = 0; index < respawnPoint.GetNodeCount(); index++) { Vector3 distance = objToRespawn.transform.position - respawnPoint.GetNode(index); float sqrMagn = distance.sqrMagnitude; if (sqrMagn < minDistance) { minDistance = sqrMagn; minDistanceNode = index; } } return(minDistanceNode); }
void DebugDraw() { if (trackNode.GetNodeCount() <= 0) { return; } if (trackNode != null) { bool indexSwitch = false; for (int index = 0; index < trackNode.GetNodeCount(); index++) { if (index == 0) { continue; } Color col; if (indexSwitch) { col = Color.red; } else { col = Color.cyan; } indexSwitch = !indexSwitch; Debug.DrawLine(trackNode.GetNode(index), trackNode.GetNode(index - 1), col); } if (!trackNode.isLoopOpen) { Debug.DrawLine(trackNode.GetNode(0), trackNode.GetNode(-1), Color.white); } Debug.DrawLine(trackNode.GetNode(trackNode.GetNodeCount() - 1), transform.position, Color.blue); Debug.DrawLine(trackNode.GetNode(0), transform.position, Color.yellow); Debug.DrawLine(trackNode.GetNode(trackNodeToolSettings.cycleNodeIndex), transform.position, Color.green); } }
public override void OnInspectorGUI() { TrackNodeTool trackNodeToolScript = (TrackNodeTool)target; DrawDefaultInspector(); trackNodeToolScript.FindTrackFiles(); if (GUILayout.Toggle(doDrawTrajectory, "Draw track curve")) { if (subscribeCount == 0) { SceneView.onSceneGUIDelegate += DebugDraw; subscribeCount++; } doDrawTrajectory = true; } else { while (subscribeCount > 0) { SceneView.onSceneGUIDelegate -= DebugDraw; subscribeCount--; } doDrawTrajectory = false; } useSpaceBarToSet = GUILayout.Toggle(useSpaceBarToSet, "Use Space Bar To Add"); trackNodeToolScript.trackNode.isLoopOpen = GUILayout.Toggle(trackNodeToolScript.trackNode.isLoopOpen, "Is Loop Open"); { GUILayout.BeginHorizontal(); if (GUILayout.Button("Cycle up selected node")) { CycleNode(1); } if (GUILayout.Button("Cycle down selected node")) { CycleNode(-1); } GUILayout.EndHorizontal(); if (GUILayout.Button("Select nearest node")) { int nearestIndex = FindNearestNode(); cycleNodeIndex = nearestIndex; } if (GUILayout.Button("Set handle at selected node")) { SetHandleAtIndex(cycleNodeIndex); } } GUILayout.BeginVertical(); if (GUILayout.Button("Add Node")) { TrackNode trackNode = GetTrackNode(); if (trackNode != null) { Vector3 pos = CalculatePositionAboveTheTrack(trackNodeToolScript.GetComponent <Transform> ().position); GetTrackNode().AddNode(pos); cycleNodeIndex = GetTrackNode().GetNodeCount() - 1; } } if (GUILayout.Button("Set selected node")) { Vector3 pos = CalculatePositionAboveTheTrack(trackNodeToolScript.GetComponent <Transform> ().position); TrackNode trackNode = GetTrackNode(); if (trackNode != null) { trackNode.SetNode(pos, cycleNodeIndex); } } if (GUILayout.Button("Insert Node")) { float distance = float.MaxValue; int nearestNode = 0; Transform trackNodeToolTransform = trackNodeToolScript.GetComponent <Transform>(); TrackNode trackNode = GetTrackNode(); for (int index = 0; index < trackNode.GetNodeCount(); index++) { if (distance > Vector3.SqrMagnitude(trackNodeToolTransform.position - trackNode.GetNode(index))) { distance = Vector3.SqrMagnitude(trackNodeToolTransform.position - trackNode.GetNode(index)); nearestNode = index; } } cycleNodeIndex = nearestNode; Vector3 directionForward = trackNode.GetNode(cycleNodeIndex + 1) - trackNode.GetNode(cycleNodeIndex); Vector3 directionBackward = trackNode.GetNode(cycleNodeIndex - 1) - trackNode.GetNode(cycleNodeIndex); Vector3 pos = CalculatePositionAboveTheTrack(trackNodeToolTransform.position); if (Vector3.Dot(directionForward, trackNodeToolTransform.position - trackNode.GetNode(cycleNodeIndex)) > Vector3.Dot(directionBackward, trackNodeToolTransform.position - trackNode.GetNode(cycleNodeIndex))) { trackNode.InsertNode(pos, cycleNodeIndex + 1); } else { trackNode.InsertNode(pos, cycleNodeIndex); } } if (GUILayout.Button("Remove Node")) { TrackNode trackNode = GetTrackNode(); trackNode.DeleteNode(cycleNodeIndex); if (cycleNodeIndex >= trackNode.GetNodeCount()) { cycleNodeIndex = trackNode.GetNodeCount() - 1; } else if (cycleNodeIndex < 0) { cycleNodeIndex = 0; } } GUILayout.EndVertical(); GUILayout.BeginHorizontal(); if (GUILayout.Button("Save track node to file")) { trackNodeToolScript.trackNode.Save(trackNodeToolScript.GetFileName()); } if (GUILayout.Button("Load track node from file")) { trackNodeToolScript.trackNode.LoadFile(trackNodeToolScript.GetFileName()); } GUILayout.EndHorizontal(); if (doDrawTrajectory) { GUILayout.TextField("Debug Draw color legend\n\tMain Track : RED and CYAN\n\tSelected node : GREEN\n\tFirst Node : YELLOW\n\tLast Node : BLUE"); } if (useSpaceBarToSet) { GUILayout.TextField("Use Space Bar To Add\n\tDrag the tool around the scene and press spacebar to add a new node.\n\tTip : Place the scene in isometric view with a top view."); } SceneView.RepaintAll(); }
int FindNearestNode() { TrackNodeTool trackNodeToolScript = (TrackNodeTool)target; TrackNode trackNode = trackNodeToolScript.trackNode; Vector3 result = Vector3.zero; int ind = -1; if (trackNode != null) { Transform trackNodeToolTransform = trackNodeToolScript.GetComponent <Transform>(); float distance = float.MaxValue; for (int index = 0; index < trackNode.GetNodeCount(); index++) { float possibleDistance = Vector3.Distance(trackNodeToolTransform.position, trackNode.GetNode(index)); if (possibleDistance < distance) { distance = possibleDistance; result = trackNodeToolTransform.position; ind = index; } } } return(ind); }
public override void OnInspectorGUI() { TrackNodeTool trackNodeToolScript = (TrackNodeTool)target; EditorUtility.SetDirty(trackNodeToolScript.trackNode); EditorUtility.SetDirty(trackNodeToolScript.trackNodeToolSettings); DrawDefaultInspector(); trackNodeToolScript.FindTrackFiles(); doDrawTrajectory = trackNodeToolScript.trackNodeToolSettings.doDrawTrajectory; doDrawTrajectory = GUILayout.Toggle(doDrawTrajectory, "Draw track curve"); useSpaceBarToSet = GUILayout.Toggle(useSpaceBarToSet, "Use Space Bar To Add"); trackNodeToolScript.trackNode.isLoopOpen = GUILayout.Toggle(trackNodeToolScript.trackNode.isLoopOpen, "Is Loop Open"); GUILayout.BeginHorizontal(); if (GUILayout.Button("Cycle up selected node")) { CycleNode(1); } if (GUILayout.Button("Cycle down selected node")) { CycleNode(-1); } GUILayout.EndHorizontal(); if (GUILayout.Button("Select nearest node")) { int nearestIndex = FindNearestNode(); CycleNodeIndex = nearestIndex; } if (GUILayout.Button("Set handle at selected node")) { SetHandleAtIndex(CycleNodeIndex); } GUILayout.BeginVertical(); if (GUILayout.Button("Add Node")) { TrackNode trackNode = GetTrackNode(); if (trackNode != null) { Vector3 pos = trackNodeToolScript.GetComponent <Transform>().position; GetTrackNode().AddNode(pos); CycleNodeIndex = GetTrackNode().GetNodeCount() - 1; } } if (GUILayout.Button("Set selected node")) { Vector3 pos = trackNodeToolScript.GetComponent <Transform>().position; TrackNode trackNode = GetTrackNode(); if (trackNode != null) { trackNode.SetNode(pos, CycleNodeIndex); } } if (GUILayout.Button("Insert Node")) { float distance = float.MaxValue; int nearestNode = 0; Transform trackNodeToolTransform = trackNodeToolScript.GetComponent <Transform>(); TrackNode trackNode = GetTrackNode(); for (int index = 0; index < trackNode.GetNodeCount(); index++) { if (distance > Vector3.SqrMagnitude(trackNodeToolTransform.position - trackNode.GetNode(index))) { distance = Vector3.SqrMagnitude(trackNodeToolTransform.position - trackNode.GetNode(index)); nearestNode = index; } } CycleNodeIndex = nearestNode; Vector3 directionForward = trackNode.GetNode(CycleNodeIndex + 1) - trackNode.GetNode(CycleNodeIndex); Vector3 directionBackward = trackNode.GetNode(CycleNodeIndex - 1) - trackNode.GetNode(CycleNodeIndex); Vector3 pos = trackNodeToolTransform.position; if (Vector3.Dot(directionForward, trackNodeToolTransform.position - trackNode.GetNode(CycleNodeIndex)) > Vector3.Dot(directionBackward, trackNodeToolTransform.position - trackNode.GetNode(CycleNodeIndex))) { CycleNodeIndex = CycleNodeIndex + 1; } trackNode.InsertNode(pos, CycleNodeIndex); } if (GUILayout.Button("Remove Node")) { TrackNode trackNode = GetTrackNode(); trackNode.DeleteNode(CycleNodeIndex); if (CycleNodeIndex >= trackNode.GetNodeCount()) { CycleNodeIndex = trackNode.GetNodeCount() - 1; } else if (CycleNodeIndex < 0) { CycleNodeIndex = 0; } } GUILayout.EndVertical(); GUILayout.BeginHorizontal(); if (GUILayout.Button("Save track node to file")) { trackNodeToolScript.trackNode.Save(trackNodeToolScript.GetFileName(), trackNodeToolScript.saveLoad.dataPath); } if (GUILayout.Button("Load track node from file")) { trackNodeToolScript.trackNode.Load(trackNodeToolScript.GetFileName(), trackNodeToolScript.saveLoad.dataPath); } GUILayout.EndHorizontal(); if (trackNodeToolScript.sourceBesierSpline == null) { GUILayout.TextField("Drag a reference to a bezier spline to import track from bezier."); } else { if (GUILayout.Button("Import Track from bezier")) { TrackNode trackNode = GetTrackNode(); trackNodeToolScript.PopulateTrackNodeWithBesier(trackNodeToolScript.PointsFromBezier); for (int i = 0; i < trackNodeToolScript.PointsFromBezier; i++) { trackNode.SetNode(trackNode.GetNode(i), i); } } } GUILayout.BeginHorizontal(); GUILayout.TextField("Selected Node number"); CycleNodeIndex = EditorGUILayout.IntField(CycleNodeIndex); GUILayout.EndHorizontal(); if (doDrawTrajectory) { GUILayout.TextField("Debug Draw color legend\n\tMain Track : RED, CYAN and WHITE (if track node is a closed loop) \n\tSelected node : GREEN\n\tFirst Node : YELLOW\n\tLast Node : BLUE"); } if (useSpaceBarToSet) { GUILayout.TextField("Use Space Bar To Add\n\tDrag the tool around the scene and press spacebar to add a new node.\n\tTip : Place the scene in isometric view with a top view."); } trackNodeToolScript.trackNodeToolSettings.doDrawTrajectory = doDrawTrajectory; trackNodeToolScript.trackNodeToolSettings.cycleNodeIndex = cycleNodeIndex; SceneView.RepaintAll(); }