/** Called when a requested path has finished calculation. * A path is first requested by #SearchPath, it is then calculated, probably in the same or the next frame. * Finally it is returned to the seeker which forwards it to this function.\n */ public virtual void OnPathComplete(Path _p) { ABPath p = _p as ABPath; if (p == null) { throw new System.Exception("This function only handles ABPaths, do not use special path types"); } canSearchAgain = true; // Claim the new path p.Claim(this); // Path couldn't be calculated of some reason. // More info in p.errorLog (debug string) if (p.error) { p.Release(this); return; } // Release the previous path if (path != null) { path.Release(this); } // Replace the old path path = p; // Make sure the path contains at least 2 points if (path.vectorPath.Count == 1) { path.vectorPath.Add(path.vectorPath[0]); } interpolator.SetPath(path.vectorPath); // REPLACEMENT // var graph = AstarData.GetGraph(path.path[0]) as ITransformedGraph; // movementPlane = graph != null ? graph.transform : GraphTransform.identityTransform; var graphRotation = new Vector3(-90, 0, 0); movementPlane = new GraphTransform(Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(graphRotation), Vector3.one)); // Reset some variables TargetReached = false; // Simulate movement from the point where the path was requested // to where we are right now. This reduces the risk that the agent // gets confused because the first point in the path is far away // from the current position (possibly behind it which could cause // the agent to turn around, and that looks pretty bad). interpolator.MoveToLocallyClosestPoint((GetFeetPosition() + p.originalStartPoint) * 0.5f); interpolator.MoveToLocallyClosestPoint(GetFeetPosition()); }
/** Called when a requested path has been calculated. * A path is first requested by #UpdatePath, it is then calculated, probably in the same or the next frame. * Finally it is returned to the seeker which forwards it to this function. */ protected override void OnPathComplete (Path newPath) { ABPath p = newPath as ABPath; if (p == null) throw new System.Exception("This function only handles ABPaths, do not use special path types"); waitingForPathCalculation = false; // Increase the reference count on the new path. // This is used for object pooling to reduce allocations. p.Claim(this); // Path couldn't be calculated of some reason. // More info in p.errorLog (debug string) if (p.error) { p.Release(this); return; } // Release the previous path. if (path != null) path.Release(this); // Replace the old path path = p; // Make sure the path contains at least 2 points if (path.vectorPath.Count == 1) path.vectorPath.Add(path.vectorPath[0]); interpolator.SetPath(path.vectorPath); var graph = AstarData.GetGraph(path.path[0]) as ITransformedGraph; movementPlane = graph != null ? graph.transform : (rotationIn2D ? new GraphTransform(Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 270, 90), Vector3.one)) : GraphTransform.identityTransform); // Reset some variables reachedEndOfPath = false; // Simulate movement from the point where the path was requested // to where we are right now. This reduces the risk that the agent // gets confused because the first point in the path is far away // from the current position (possibly behind it which could cause // the agent to turn around, and that looks pretty bad). interpolator.MoveToLocallyClosestPoint((GetFeetPosition() + p.originalStartPoint) * 0.5f); interpolator.MoveToLocallyClosestPoint(GetFeetPosition()); // Update which point we are moving towards. // Note that we need to do this here because otherwise the remainingDistance field might be incorrect for 1 frame. // (due to interpolator.remainingDistance being incorrect). interpolator.MoveToCircleIntersection2D(position, pickNextWaypointDist, movementPlane); var distanceToEnd = remainingDistance; if (distanceToEnd <= endReachedDistance) { reachedEndOfPath = true; OnTargetReached(); } }
public bool Move() { var position = transform.position; _interpolator.MoveToLocallyClosestPoint(position, true, false); _interpolator.MoveToCircleIntersection2D(position, 0.5f, _movementPlane); var target = _interpolator.position; Vector2 dir = target - position; var distance = dir.magnitude + _interpolator.remainingDistance; if (distance <= 1.0f) { return(false); } _enemy.AddSpringForce(dir, distance); return(true); }
/** Called when a requested path has been calculated. * A path is first requested by #UpdatePath, it is then calculated, probably in the same or the next frame. * Finally it is returned to the seeker which forwards it to this function. */ public override void OnPathComplete(Path newPath) { ABPath p = newPath as ABPath; if (p == null) { throw new System.Exception("This function only handles ABPaths, do not use special path types"); } waitingForPathCalculation = false; // Increase the reference count on the new path. // This is used for object pooling to reduce allocations. p.Claim(this); // Path couldn't be calculated of some reason. // More info in p.errorLog (debug string) if (p.error) { p.Release(this); return; } // Release the previous path. if (path != null) { path.Release(this); } // Replace the old path path = p; // Make sure the path contains at least 2 points if (path.vectorPath.Count == 1) { path.vectorPath.Add(path.vectorPath[0]); } interpolator.SetPath(path.vectorPath); var graph = AstarData.GetGraph(path.path[0]) as ITransformedGraph; movementPlane = graph != null ? graph.transform : GraphTransform.identityTransform; // Reset some variables targetReached = false; // Simulate movement from the point where the path was requested // to where we are right now. This reduces the risk that the agent // gets confused because the first point in the path is far away // from the current position (possibly behind it which could cause // the agent to turn around, and that looks pretty bad). interpolator.MoveToLocallyClosestPoint((GetFeetPosition() + p.originalStartPoint) * 0.5f); interpolator.MoveToLocallyClosestPoint(GetFeetPosition()); var distanceToEnd = movementPlane.ToPlane(steeringTarget - position).magnitude + interpolator.remainingDistance; if (distanceToEnd <= endReachedDistance) { targetReached = true; OnTargetReached(); } }
protected new void Update() { base.Update(); if (Role == CharacterRole.Drone && !_isSearchingPath && Input.GetButtonDown("Fire2")) { var ray = Camera.main.ScreenPointToRay(Input.mousePosition); Vector3 point = ray.IntersectXY(); Destination = point; _seeker.StartPath(transform.position, Destination); _isSearchingPath = true; } { Vector2 direction; if (Role == CharacterRole.Main) { direction.x = Input.GetAxisRaw(_hAxis); direction.y = Input.GetAxisRaw(_vAxis); if (Mathf.Abs(direction.x) < DeadZone) { direction.x = 0.0f; } else { direction.x = Mathf.Sign(direction.x); } if (Mathf.Abs(direction.y) < DeadZone) { direction.y = 0.0f; } else { direction.y = Mathf.Sign(direction.y); } Velocity += direction * Acceleration * Time.deltaTime; } else { if (!_interpolator.valid || _isAtDestination) { Velocity = Vector3.zero; } else { var position = transform.position; _interpolator.MoveToLocallyClosestPoint(position, true, false); _interpolator.MoveToCircleIntersection2D(position, 0.5f, _movementPlane); var target = _interpolator.position; Vector2 dir = target - position; var distance = dir.magnitude + _interpolator.remainingDistance; if (distance <= StopThreshold) { _isAtDestination = true; } else { AddSpringForce(dir, distance); } } } } if (Input.GetButton("Fire1") && _game.Energy >= _game.EnergyNeedForFire) { var ray = Camera.main.ScreenPointToRay(Input.mousePosition); Vector3 point = ray.IntersectXY(); var position = transform.position; var direction = (point - position).normalized; position += direction * 0.32f; var points = _laserLines[0].points3; points.Clear(); points.Add(position); for (var i = 0; i < 2; i++) { var hit = Physics2D.Raycast(position, direction, 10.0f, _laserLayerMask); if (hit.collider == null) { points.Add(position + direction * 10.0f); break; } points.Add(hit.point); var enemy = hit.collider.GetComponent <Enemy>(); if (enemy != null) { enemy.DoLaserHit(_game.LaserDamage * Time.deltaTime); float ed = _game.EnergyDrain * Time.deltaTime; if (_game.Energy >= ed && ed >= 0) { _game.Energy -= ed; _game.EnergySpent += ed; } else { _game.Energy = 0; } break; } direction = Vector2.Reflect(direction, hit.normal); position = (Vector3)hit.point + direction * 0.05f; } //var laserPosition = position; //laserPosition.z -= 5.0f; //_laserParticles.transform.position = laserPosition; //var rotDir = new Vector3(-direction.y, direction.x); //_laserParticles.transform.rotation = // Quaternion.Euler(0.0f, 0.0f, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg); //var scale = Vector3.one * 0.08f; //scale.x = Vector3.Distance(point, position) * scale.x; //_laserParticles.transform.localScale = scale; //_laserParticles.SetActive(true); _laserLines[0].active = true; _laserLines[0].Draw(); if (_as.Length > 0 && _as[0].clip != GameManager.Instance.LaserStartClip) { _as[0].clip = GameManager.Instance.LaserStartClip; _as[0].Play(); _as[1].clip = GameManager.Instance.LaserShotClip; _as[1].Play(); } } else { if (_as.Length > 0 && _as[0].clip != GameManager.Instance.LaserEndClip) { _as[0].clip = GameManager.Instance.LaserEndClip; _as[0].Play(); _as[1].Stop(); } _laserLines[0].active = false; //_laserParticles.SetActive(false); } }