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 P.W. · Feb 12, 2015 at 10:31 AM · unity 2dai

Make enemy to "see" player in Unity 2d

Hello,

I'm making top down game (Hotline Miami style).

My problem is: how to make raycast or linecast (to detect player) form enemy eyes. Enemy can turn to face player after he detect him, so raycast from must rotate with enemy to emulate enemy vision direction.

To explain more: My problem is: how to draw a raycast in front of the enemy (just like from enemy's eyes), and rotate it when enemy rotates.

Checking if a character see something is not a problem.

Comment
Add comment · Show 7
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 Doireth · Feb 12, 2015 at 10:32 AM 0
Share

Your question is unclear. If you want to check if a character "sees" something is, like you said, to use a raycast (see Raycast2D

I'm unclear what you are actually asking. Could you explain further?

avatar image P.W. · Feb 12, 2015 at 10:44 AM 0
Share

$$anonymous$$y problem is: how to draw a raycast in front of the enemy (just like from enemy's eyes), and rotate it when enemy rotates.

Checking if a character see something is not a problem.

avatar image hexagonius · Feb 12, 2015 at 11:52 AM 0
Share

A raycast ray usually has a direction for casting. whatever forward your character is using (in 2D might be y if you start looking up in the top down game if the camera is not acutally looking dawn in 3D), use it as the direction. If you need to be more precise on the origin, use the position of the characters eyes for example.

  RaycastHit2D hit = Physics2D.Raycast((Vector2) transform.position, (Vector2) transform.up);

should do the trick

avatar image P.W. · Feb 12, 2015 at 01:16 PM 0
Share

Unfortunately

 RaycastHit2D hit = Physics2D.Raycast((Vector2) transform.position, (Vector2) transform.up); 

does not work for me. I don't know how to change raycast lenght. Also raycast collide with enemy body, and do not hit the player.

To be more precise here is the picture: alt text

  • enemy always starts facing "Up" on y-axis,

  • Raycast turns with enemy,

  • I want to be able to change raycast lenght,

Thanks,

ex.png (4.8 kB)
avatar image El Maxo · Feb 12, 2015 at 02:36 PM 0
Share

you could attach the raycast to a empty game object just infront of where u think the eyes will be

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by P.W. · Feb 14, 2015 at 01:49 PM

Ok, i upgrade the solution with 'fov' and simple vision cone form my enemies.

 distanceToPlayer = Vector3.Distance ((Vector2)transform.position, (Vector2)playerPosition.position);
         
 angle = Vector2.Angle (transform.up, playerPosition.position - transform.position);
     
         if (distanceToPlayer < rng)  //range = known float
         {
             playerInRange = true;
             if(angle < fov*0.5f) //fov = know float
             {
 
             // do something
     
             }


It's worked form me well.

Thanks everybody for answer.

Comment
Add comment · Share
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
0

Answer by ryandotdee · Feb 12, 2015 at 03:07 PM

transform.forward will give you the direction the object is facing

  RaycastHit2D hit = Physics2D.Raycast((Vector2) transform.position, (Vector2) transform.forward, distance);

 

http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

3rd argument is distance ( length)

Comment
Add comment · Share
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
0

Answer by brentshermana · Aug 10, 2015 at 04:04 AM

Contrary to what ryandotdee says, in top-down perspective it's transform.up that gives you the direction your character is pointing (provided that the original imported sprite is facing upwards)

Similarly, a sprite which faces downwards in the sprite sheet would use transform.down

Comment
Add comment · Share
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

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

7 People are following this question.

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

Related Questions

A* Pathfinding Enemy AI, How to search for something else when no path to player available 0 Answers

Does anybody know how to make it to where the enemy just stops short? 2 Answers

Brackeys EnemyAi Script 0 Answers

How to make player hides from an enemy ? 0 Answers

Set AI Destination Setter target from Script 2 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