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 /
This question was closed Sep 06, 2015 at 07:40 PM by legetic.
avatar image
0
Question by legetic · Sep 07, 2015 at 03:46 AM · c#raycastenemydetectionraycasthit2d

2d raycast enemy detection problem.

So i've been trying to solve this problem for about 2-3 days now and no matter how many guides i look at i cant seem to solve it... i am making a 2D game and am trying to use raycasting as the enemy's playerdetection. i have commented the code and tried to make it as clear as possible so i am hoping someone could check it out and that the code speaks for itself. I included the whole code so the bottom section is most likely not a part of the problem since it's been working. Thanks in advance! /C# Newbie

 private Transform myTransform;//my (the enemy's)transform

 public Transform target;//The player't transform

 public int maxDistance;//detection distance

 public int moveSpeed = 3;//movement speed

 public int Direction;//direction of movement and sprite
 
 void Awake(){
     myTransform = transform;//init my transform
 }
 
 
 void Start () {
     GameObject go = GameObject.FindGameObjectWithTag("Player");//init player


     target = go.transform;//init player transform
     
     maxDistance = 10;//init detectiondistance
 }
 
 
 void Update () {

     RaycastHit hit;//init raycast hit to store info about the object that was hit

     Debug.DrawRay(target.position, myTransform.position - target.position, Color.red); //debug ray to see the ray

     Ray detectionRay = new Ray (target.position, myTransform.position - target.position);//the ray that will be used (Ray2D doesn't seem to work)

     if (Physics.Raycast(detectionRay,out hit))//this is makes me kind of puzzled because it seems like im only asking if the ray is cast but idk.
     {
         Debug.Log(hit.collider.name+", "+hit.collider.tag);
         Debug.Log("NOOOOOOOOOOHIT");//even this doesn't seem to show up in console. kept this her because it would be a first step to get this one working.
     
         
         if (hit.collider.gameObject.name == "Player")//if the object that was hit goes by the name "Player" then:
         {
             Debug.Log("HIT");//write HIT
         }
     }



     //everything under here seems to work fine and are most likely not a part of the problem. pasted them too just in case.













     if (target.position.x > myTransform.position.x +1) {
     
         Direction = 1;
     } else if(target.position.x < myTransform.position.x -1){
     
         Direction = -1;
     }

     
     if(Vector2.Distance(target.position, myTransform.position) < maxDistance){
         //Move towards target
     
         myTransform.position += myTransform.right * moveSpeed * Direction * Time.deltaTime;
         

     }

     gameObject.transform.localScale = new Vector2 (Direction, 1);


 }
Comment
Add comment · Show 1
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
avatar image legetic · Sep 06, 2015 at 07:32 PM 0
Share

And yes i know the ray wont affect the enemy's movement as it currently is. Right now i simply want the ray to work and collide with stuff and in case it collides with the player i should get some sort of indication (debug message).

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

2d Raycast problem from enemy to player C# 1 Answer

DetectSphere - No physics 1 Answer

How to make enemy check if it doesn`t see player for certain times it changes state 1 Answer

Multiple Cars not working 1 Answer

"Operator `??' cannot be applied to operands of type `bool' and `bool'" Error with C# 6's "??" operator 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