Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by JekasG · Jan 01, 2015 at 01:37 AM · c#raycastplayervector3

Problems with Raycasting Requirements

Im working on a script which kinda works like an Enemy AI. Its kinda like a detection system with requirements that are required to be met.

This is a recent question i asked using my code http://gamedev.stackexchange.com/questions/90329/raycast-flashing-problem

So this is the how it works:

enter image description here

 using UnityEngine;
 using System.Collections;

 public class Script_v2 : MonoBehaviour {

     // Player Properties
     private GameObject player;
     public Vector3 playerSize;
     private Vector3 playerTransform;
     public Vector3 playerTransformTL;
     public Vector3 playerTransformTR;
     public Vector3 playerTransformBL;
     public Vector3 playerTransformBR;

     private Vector3 newPlayerTransformTL;
     private Vector3 newPlayerTransformTR;

     private Vector3[] playerRaycastPoints;


     // Enemy Properties
     private Vector3 enemyTransformTL;
     private Vector3 enemyTransformTR;
     private Vector3 enemyTransformBL;
     private Vector3 enemyTransformBR;

     public float distance;
     public Vector3 enemySize;

     // Detection Alerts
     public bool outOfVision;
     public bool alerted;
     public bool alertedLock;
     public bool dead;

     Ray ray;
     RaycastHit hit;

     // Use this for initialization
     void Start () {
         playerRaycastPoints = new Vector3[4];

         distance = 3f;
         player = GameObject.FindGameObjectWithTag ("Player");


     }

     // Update is called once per frame
     void Update () {

         enemyTransformTL = new Vector3 (transform.position.x - 0.5f, transform.position.y + 0.5f, transform.position.z);
         enemyTransformTR = new Vector3 (transform.position.x + 0.5f, transform.position.y + 0.5f, transform.position.z);


         enemyTransform_TL_TR ();
         detectionAlert ();
         Reference_Player_Transform_Points ();

         Debug.Log (alerted + " " + alertedLock);

     }

     void OnDrawGizmos() {
         Gizmos.color = Color.blue;
         Gizmos.DrawWireSphere (new Vector3(transform.position.x - 0.5f, transform.position.y + 0.5f, transform.position.z), distance);
         Gizmos.DrawWireSphere (new Vector3(transform.position.x + 0.5f, transform.position.y + 0.5f, transform.position.z), distance);
     }

     public void enemyTransform_TL_TR() {

         alerted = true;
         alertedLock = false;
         for (int i = 0; i < 4; i++) {

             double enemyAngleTL = Mathf.Atan2(playerRaycastPoints[i].y - ( transform.position.y + 0.5f ),
                                               playerRaycastPoints[i].x - ( transform.position.x - 0.5f )) * 180f / 3.14159265f;
             //Debug.Log (enemyAngleTL);
             double enemyAngleTR = Mathf.Atan2 (playerRaycastPoints[i].y - (transform.position.y + 0.5f),
                                                playerRaycastPoints[i].x - (transform.position.x + 0.5f)) * 180f / 3.14159265f;
             
             Vector3 directionTL = (playerRaycastPoints[i] - enemyTransformTL).normalized;
             Ray rayTL = new Ray(enemyTransformTL, directionTL);
             RaycastHit hitTL;
             Vector3 directionTR = (playerRaycastPoints[i] - enemyTransformTR).normalized;
             Ray rayTR = new Ray (enemyTransformTR, directionTR);
             RaycastHit hitTR;


             //Debug.DrawRay (rayTR.origin, rayTR.direction * distance, Color.yellow);

             if(Physics.Raycast (rayTL, out hitTL, distance)) {
                 if((enemyAngleTL > 90 && enemyAngleTL < 180)) {
                     Debug.DrawRay (rayTL.origin, rayTL.direction * distance, Color.yellow);
                     alerted = false;



                 }
                 else {

                     alertedLock = true;
                 }

             }



         }
         //alertedLock = true;




     }

     public void detectionAlert() {
         if (alerted == false && alertedLock == false) {
             gameObject.renderer.material.color = Color.green;
         }
         if (alerted == false && alertedLock == true) {
             gameObject.renderer.material.color = Color.magenta;        
         }
         if(alerted == true && alertedLock == false) {
             gameObject.renderer.material.color = Color.blue;
         }
         if(alerted == true && alertedLock == true) {
             gameObject.renderer.material.color = Color.red;
         }
         else {
             //gameObject.renderer.material.color = Color.red;
         }
     }

     private void Reference_Player_Transform_Points() {

         playerSize = player.transform.localScale;

         playerTransformTL = new Vector3(player.transform.position.x - (playerSize.x / 2),
                                         player.transform.position.y + playerSize.y  / 2,
                                         player.transform.position.z);
         playerTransformTR = new Vector3(player.transform.position.x + (playerSize.x / 2),
                                         player.transform.position.y + playerSize.y  / 2,
                                         player.transform.position.z);
         playerTransformBL = new Vector3(player.transform.position.x - (playerSize.x / 2),
                                         player.transform.position.y - playerSize.y  / 2,
                                         player.transform.position.z);
         playerTransformBR = new Vector3(player.transform.position.x + (playerSize.x / 2),
                                         player.transform.position.y - playerSize.y  / 2,
                                         player.transform.position.z);

         playerRaycastPoints [0] = playerTransformTL;
         playerRaycastPoints [1] = playerTransformTR;
         playerRaycastPoints [2] = playerTransformBL;
         playerRaycastPoints [3] = playerTransformBR;

         /*
             Debug.Log (playerTransformTL);
             Debug.Log (playerTransformTR);
             Debug.Log (playerTransformBL);
             Debug.Log (playerTransformBR);
         */
     }

     

 }

The problem with this code is that. If you look in the diagram. alertLock should be false when its outside the greenZone. But for some reason it doesnt. When the player is fully inside the greenZone, the enemy turns green.

I dont know why. Someone please help me.

Thanks in advance.

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

2 People are following this question.

avatar image avatar image

Related Questions

Get RaycastHit Local Coords (C#) 1 Answer

How to make "enemy" have a frustum(cone) view and not a circle radius 2 Answers

Snapping object to raycast hit in 1 local axis. 1 Answer

Player rotation 0 Answers

Locking gameobject to rotating floor 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