void OnTriggerEnter2D(Collider2D other) { GrandmaController granny = other.GetComponent <GrandmaController>(); granny.MakeSpeechBubble("A pair of slippers. Maybe I can throw one to make a noise."); if (granny != null) { Destroy(this.gameObject); granny.slipperCount++; } }
void LateUpdate() { //rotate vision cone accordingly. Vector2 curentVelocity = rigidbody.velocity; float goalAngle = 0; if (vision.CanSeePlayer()) { goalAngle = Vector2.SignedAngle(GrandmaController.GetInstance().transform.position - this.transform.position, Vector2.right); } else { goalAngle = Vector2.SignedAngle(curentVelocity, Vector2.right); goalAngle = goalAngle / 8; goalAngle = Mathf.Round(goalAngle); goalAngle *= 8; } float currentAngle = this.transform.rotation.eulerAngles.z; float angleChange = rotSpeed * Time.deltaTime; float angleDistance = AngleDifference(currentAngle, goalAngle); if (angleDistance < 0) { angleChange *= -1; } if (Mathf.Abs(angleChange) > Mathf.Abs(angleDistance)) { this.transform.rotation = Quaternion.Euler(0, 0, goalAngle); } else { this.transform.rotation = Quaternion.Euler(0, 0, currentAngle + angleChange); } //Debug.Log("Current: " + currentAngle + " Goal: " + goalAngle + " Change: " + angleChange); DrawFieldOfView(); }
public void Update() { if (vision.CanSeePlayer()) { timeGrannyLastSeen = Time.time; } if (path == null) { // We have no path to follow yet, so don't do anything return; } // Check in a loop if we are close enough to the current waypoint to switch to the next one. // We do this in a loop because many waypoints might be close to each other and we may reach // several of them in the same frame. reachedEndOfPath = false; // The distance to the next waypoint in the path float distanceToWaypoint; while (true) { // If you want maximum performance you can check the squared distance instead to get rid of a // square root calculation. But that is outside the scope of this tutorial. distanceToWaypoint = Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]); if (distanceToWaypoint < nextWaypointDistance) { // Check if there is another waypoint or if we have reached the end of the path if (currentWaypoint + 1 < path.vectorPath.Count) { currentWaypoint++; } else { // Set a status variable to indicate that the agent has reached the end of the path. // You can use this to trigger some special code if your game requires that. reachedEndOfPath = true; break; } } else { break; } } // Slow down smoothly upon approaching the end of the path // This value will smoothly go from 1 to 0 as the agent approaches the last waypoint in the path. var speedFactor = reachedEndOfPath ? Mathf.Sqrt(distanceToWaypoint / nextWaypointDistance) : 1f; // Direction to the next waypoint // Normalize it so that it has a length of 1 world unit Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized; // Multiply the direction by our desired speed to get a velocity Vector3 velocity = dir * speed * speedFactor; if (Time.time - timeGrannyLastSeen < this.followTime) { followingGranny = true; path = null; seeker.StartPath(transform.position, GrandmaController.GetInstance().transform.position, OnPathComplete); } else if (followingGranny && Time.time - timeGrannyLastSeen >= followTime) { followingGranny = false; path = null; seeker.StartPath(transform.position, targetPositions[targetPositionIndex].position, OnPathComplete); } else if (isDistracted && reachedEndOfPath) { velocity = dir * 0; if (distractedTime == 0.0f) { distractedTime = Time.time; } else if (Time.time - distractedTime >= totalDistractedTime) { distractedTime = 0.0f; isDistracted = false; targetPositionIndex--; PathToNextMarker(); } } else if (reachedEndOfPath) { path = null; PathToNextMarker(); } // Move the agent // Note that SimpleMove takes a velocity in meters/second, so we should not multiply by Time.deltaTime GetComponent <Rigidbody2D>().velocity = velocity; }