Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 ParyPro · Jan 10, 2019 at 09:05 PM · aienemylevel

Guard AI is off - Please Help!

Hello! I am still very new to this whole Unity thing but I'm absolutely loving it. I have encountered a big problem in my first 2d game with my Guard AI. The game design is very simple; guide the little square (player) across the platforms in the level to reach the end, with many different Guard AI spread across it shooting out a red ray that, if hit with the player, causes game over. My main Guard AI is quite simple as well - move back and forth across a single platform, when getting to the end of the platform turning around and shooting out a red beam that covers the length of the platform, causing the player to have to jump up when it shoots to avoid getting hit, with the red beam then disappearing and it moving back across to the other side of the platform. For some odd reason upon touching down to the platform anywhere to the left of the Guard, even when the beam is not out, the beam automatically and instantly shoots out and kills the player. I have been working at this problem for weeks but my simple lack of coding skills has not allowed me to locate the cause of this. I have attached the entire Guard script here:

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

public class Guard2 : MonoBehaviour {

 public Transform pathHolder;
 Transform player;
 Color originalSpotLightColour;

 public float distance;

 public LineRenderer lineOfSight;
 public Gradient redColor;
 public Gradient greenColor;

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

 public Light spotLight;
 public float viewDistance;
 public LayerMask viewMask;
 float viewAngle;

 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())
     {
         spotLight.color = Color.red;
     }
     else
     {
         spotLight.color = originalSpotLightColour;
     }

     RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.right, distance);
     if (hitInfo.collider != null)
     {
         Debug.DrawLine(transform.position, hitInfo.point, Color.red);
         lineOfSight.SetPosition(1, hitInfo.point);
         lineOfSight.colorGradient = redColor;

         if (hitInfo.collider.CompareTag("Player"))
         {
             Destroy(hitInfo.collider.gameObject);
         }

     }
     else
     {
         Debug.DrawLine(transform.position, transform.position + transform.right * distance, Color.green);
         lineOfSight.SetPosition(1, transform.position + transform.right * distance);
         lineOfSight.colorGradient = greenColor;
     }

     lineOfSight.SetPosition(0, transform.position);
 }


 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);
 }

}

I have many video visuals of this problem that are too large to attach here - if anyone would like to communicate via email to get a visual I would love to send them over! Thanks for taking the time to read and if anyone has any tips/ suggestions to solve this problem please let me know either here or email me at rsrickard@yahoo.com

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.

Follow this Question

Answers Answers and Comments

168 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Guard AI acting real weird - Please Help! 0 Answers

Trouble making jumping spider enemies 0 Answers

Trying to make an enemyAI and have a few issues could I get some help? 0 Answers

'Enemies' Dissapearing whenever I get closec 1 Answer

Attaching objects to scripts without dragging and dropping 0 Answers


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