• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ArTiC_LegendZz · Oct 27, 2018 at 12:07 AM · script error

guard script not working,Guard script not working properly?

hi, my guard is supposed to spot the player and the light turn red when the player enters the field of view of the guard and it is also supposed to display a screen that says "you were spotted" when that happens, however despite no errors in the script itself it will not do that. can someone please read through my script and see if there is anything wrong with it that might be preventing this from happening. Thanks :)

here is the script-

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Guard : MonoBehaviour {

 public static event System.Action OnGuardHasSpottedPlayer;

 public float speed = 5;
 public float waitTime = .3f;
 public float turnSpeed = 90;
 public float timeToSpotPlayer = .5f;

 public Light spotlight;
 public float viewDistance;
  public LayerMask viewMask;
  float viewAngle;
 float playerVisableTimer;

 public Transform pathHolder;
 Transform player;
 Color originalSpotlightColour;

 private void Start() {
     player = GameObject.FindGameObjectWithTag("Player").transform; 
     viewAngle = spotlight.spotAngle;
     originalSpotlightColour = spotlight.color;


     Vector3[] waypoints = new Vector3[pathHolder.childCount];
     for (int i = 0; i < waypoints.Length; i++) {
         waypoints[i] = pathHolder.GetChild(i).position;
         waypoints[i] = new Vector3(waypoints[i].x, transform.position.y, waypoints[i].z);
     }
 
     StartCoroutine(FollowPath(waypoints) );
 }

 void Update() {
     if (CanSeePlayer()) { 
     playerVisableTimer += Time.deltaTime;
     }else{
     playerVisableTimer -= Time.deltaTime;
     }
     playerVisableTimer = Mathf.Clamp (playerVisableTimer,0, timeToSpotPlayer);
     spotlight.color = Color.Lerp (originalSpotlightColour, Color.red, playerVisableTimer/timeToSpotPlayer);

     if (playerVisableTimer >= timeToSpotPlayer) {
         if (OnGuardHasSpottedPlayer != null) {
             OnGuardHasSpottedPlayer();
         }
     }
 }

 bool CanSeePlayer()
 {
     if (Vector3.Distance(transform.position, Player.Position) < viewDistance)
     {
         Vector3 dirToPlayer = (player.position - transform.position).normalized;
         float angleBetweenGuardAndPlayer = Vector3.Angle(transform.forward, dirToPlayer);
         if (angleBetweenGuardAndPlayer < viewAngle / 2f) {
             if (!Physics.Linecast(transform.position, player.position, viewMask)) {
                 return true;
             } 
           }
         }
     return false;
 }
     
 

 IEnumerator FollowPath(Vector3[] waypoints) {
     transform.position = waypoints[0];

     int targetWaypointIndex = 1;
     Vector3 targetWaypoint = waypoints[targetWaypointIndex];
     transform.LookAt(targetWaypoint);


     while (true) {
         transform.position = Vector3.MoveTowards(transform.position, targetWaypoint, speed * Time.deltaTime);
         if (transform.position == targetWaypoint)
         {
             targetWaypointIndex = (targetWaypointIndex + 1) % waypoints.Length;
             targetWaypoint = waypoints[targetWaypointIndex];
             yield return new WaitForSeconds(waitTime);
            yield return StartCoroutine(TurnToFace(targetWaypoint));

         }
         yield return null;
     }
 }

 IEnumerator TurnToFace(Vector3 lookTarget)
 {
     Vector3 dirToLookTarget = (lookTarget - transform.position).normalized;
     float targetAngle = 90 - Mathf.Atan2(dirToLookTarget.z, dirToLookTarget.x) * Mathf.Rad2Deg;
     while (Mathf.Abs(Mathf.DeltaAngle(transform.eulerAngles.y, targetAngle)) > 0.05f) { 
      float angle = Mathf.MoveTowardsAngle (transform.eulerAngles.y,targetAngle, turnSpeed * Time.deltaTime);
         transform.eulerAngles = Vector3.up * angle;
         yield return null; 
     }
 }


      void OnDrawGizmos() {
         Vector3 startPosition = pathHolder.GetChild(0).position;
         Vector3 previousPosition = startPosition;

         foreach (Transform waypoint in pathHolder)
         {
             Gizmos.DrawSphere(waypoint.position, .3f);
             Gizmos.DrawLine(previousPosition, waypoint.position);
             previousPosition = waypoint.position;
         }
         Gizmos.DrawLine(previousPosition, startPosition);
     Gizmos.color = Color.red;
     Gizmos.DrawRay(transform.position, transform.forward * viewDistance);

     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

158 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

transformers script error 1 Answer

After converting c# Script, Unity is giving errors on multiple lines which were fine before. 0 Answers

Simple script error 1 Answer

Dont destroy object when changing scene 1 Answer

Unity 5 checking if player isGrounded 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges