/// <summary> /// Use this for initialization /// </summary> void Start() { // Get the waypoints from the game objects around the track Waypoint[] WaypointsFromTrack = Track.GetComponentsInChildren <Waypoint> (); foreach (Waypoint CurrentWaypoint in WaypointsFromTrack) { // Add the waypoint to the list Waypoints.Add(new Vector2(CurrentWaypoint.GetPosition().x, CurrentWaypoint.GetPosition().y)); if (Debugging) { Instantiate(WaypointPrefab, CurrentWaypoint.GetPosition(), Quaternion.identity); } } // Create the driver game objects AIDrivers = new List <GameObject> (); for (int PlayerNum = 0; PlayerNum < NumberOfAIDrivers; PlayerNum++) { // Calculate the initial position Vector2 StartPosition = new Vector2(Mathf.Floor(((PlayerNum + 1) / 2)) * DriverStartSpacing, 0); // Stagger the drivers if (PlayerNum % 2 == 0) { StartPosition.y += -0.5f; } else { StartPosition.y += 0.5f; } // Create the driver in the UI environment GameObject NewDriver = Instantiate(PlayerPrefab, StartPosition, Quaternion.Euler(0, 0, 90)); AIDrivers.Add(NewDriver); // Create a new driver AI string DriverName = "Player " + PlayerNum.ToString(); NewDriver.GetComponent <AIPlayerController> ().Name = DriverName; NewDriver.GetComponent <AIPlayerController> ().ArriveRadius = WaypointArriveRadius; NewDriver.GetComponent <AIPlayerController> ().Waypoints = new List <Vector2> (Waypoints); NewDriver.GetComponent <AIPlayerController> ().rand = rand; NewDriver.GetComponent <AIPlayerController> ().Debugging = Debugging; NewDriver.GetComponent <AIPlayerController> ().MaxSpeed = NormalMaxSpeed; NewDriver.GetComponent <AIPlayerController> ().StuckSpeed = StuckSpeed; NewDriver.GetComponent <AIPlayerController> ().StuckTimeout = StuckTimeout; NewDriver.GetComponent <AIPlayerController> ().GetUnstuckTime = GetUnstuckTime; NewDriver.GetComponent <SpriteRenderer> ().material.SetColor("_Color", Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f)); // Get a random speed adjustment double SpeedAdjustment = (rand.NextDouble() - 0.5) * MaxSpeedAdjustmentRange; NewDriver.GetComponent <AIPlayerController> ().MaxSpeed += (float)SpeedAdjustment; // Activate the driver navigation NewDriver.GetComponent <AIPlayerController> ().NavigationActive = true; } isPaused = false; }
/// <summary> /// Move the character to a direction /// </summary> /// <param name="wayPoint"></param> public void moveToDeirection(Utility.Direction movingDirection) { if (CanMove) { var destination = CurrentWaypoint.GetComponent <Waypoint>().getDestination(movingDirection); if (destination != null) { if (destination.GetComponent <InteractiveObject>() != null) { //Debug.Log("we are interacting with " + destination.GetComponent<InteractiveObject>().registerName); Direction = movingDirection; updateAnimation(); destination.GetComponent <InteractiveObject>().interact(this); } else if (destination.GetComponent <Waypoint>() != null) { moveToWaypoint( destination, movingDirection, CurrentWaypoint.GetComponent <Waypoint>().getMoveMode(movingDirection) ); } } } }
/// <summary> /// /// </summary> /// <param name="s"></param> /// <param name="ticks">TimeSpan ticks (100ns).</param> private void ProcessNavigation(HeliState s, long ticks) { // Note: Changing current waypoint must be done after input processing, because // modifying the waypoint will not update the helicopter state until next time - // including states about angle and distance to waypoint. // Make sure we use 2D position (map) when holding height above terrain, in order to pass waypoints. if (Task.HoldHeightAboveGround > 0) { float groundHeightAtWaypoint = Map.GetAltitude(VectorHelper.ToHorizontal(CurrentWaypoint.Position)); CurrentWaypoint.Position.Y = groundHeightAtWaypoint + Task.HoldHeightAboveGround; } bool isWithinRadius = IsTestMode ? IsTruePositionWithinRadius : CurrentWaypoint.IsWithinRadius(s.Position); if (isWithinRadius) { CurrentWaypoint.SecondsWaited += ticks / 1000.0f; } else { CurrentWaypoint.SecondsWaited = 0; // Seconds waited should be in an uninterrupted sequence } // Progress to next waypoint if currently at start point or have been sufficiently long at a stop point if ( //CurrentWaypoint.Type == WaypointType.Start || CurrentWaypoint.Type == WaypointType.Hover && CurrentWaypoint.DoneWaiting || CurrentWaypoint.Type == WaypointType.Intermediate && isWithinRadius) { Task.Next(); } else if (CurrentWaypoint.Type == WaypointType.TestDestination && isWithinRadius) { if (Task.Loop) { Task.Next(); } else { IsAtDestination = true; } } if (Task.Current == null) { throw new Exception("Waypoint should never be null! Always end task with an end-waypoint."); } // s.Waypoint = Task.Current; // s.HPositionToGoal = VectorHelper.ToHorizontal(s.Waypoint.Position - s.Position); }
protected Location GetNextWayPoint() { Location Next = null; //get the next waypoint // The only criteria is that the next waypoint be at least 3 yards away from current // if all fail then skip to end while (TravelPath.GetFirst() != null) { //get next waypoint and remove it from the list at the same time Next = TravelPath.RemoveFirst(); //check distance to the waypoint float distance = CurrentWaypoint.GetDistanceTo(Next); //if distance greater then 3f then return this waypoint if (distance > 10f) { break; } } return(Next); }
private void RunWayTrail(TweenEvent te) { if (_wayTrail == null) { return; } var currentWaypoint = te != null && te.parms[0] != null ? (Waypoint)te.parms[0] : CurrentWaypoint; if (currentWaypoint.IsControlPoint && currentWaypoint.GetInstanceID() != CurrentWaypoint.GetInstanceID()) { currentWaypoint = CurrentWaypoint; } if (currentWaypoint.NextWaypoint == null || BrushMode == BrushModeType.Autoplay) { _wayTrail.SetActive(false); return; } var currentWpPos = currentWaypoint.transform.localPosition; var initPos = new Vector3(currentWpPos.x, currentWpPos.y, _wayTrail.transform.localPosition.z); _wayTrail.transform.localPosition = initPos; var nextWpPos = currentWaypoint.NextWaypoint.transform.localPosition; var nextPos = new Vector3(nextWpPos.x, nextWpPos.y, initPos.z); #region animator rotatation calculating var sinA = Vector3.Magnitude(Vector3.Project(nextPos - initPos, Vector3.left)) / Vector3.Magnitude(nextPos - initPos); if (nextWpPos.y <= currentWpPos.y) { sinA = (nextWpPos.x < currentWpPos.x) ? -sinA : sinA; } else if (nextWpPos.x > currentWpPos.x) { sinA = -sinA; } var angle = Mathf.Asin(sinA) * 180 / Mathf.PI; if (nextWpPos.y > currentWpPos.y) { angle = angle + 180; } _wayTrail.transform.localRotation = Quaternion.Euler(0, 0, angle); #endregion var distance = Vector3.Distance(new Vector2(currentWpPos.x, currentWpPos.y), new Vector2(nextWpPos.x, nextWpPos.y)); var param = new TweenParms(); param.Prop("localPosition", nextPos); param.Ease(EaseType.Linear); param.OnComplete(RunWayTrail, currentWaypoint.NextWaypoint); _wayTrail.gameObject.SetActive(TapGesture.State == TouchScript.Gestures.Gesture.GestureState.Possible); HOTween.To(_wayTrail.transform, distance / _wayTrailSpeed, param); }