• 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
1
Question by jo90 · Feb 03, 2012 at 04:10 AM · ai

AI Question

Hello all i was wondering how do i make it that a enemy has some sort of line looking towards the player and if the line gets blocked by a colider it will, //do somthing

thanks in adcanced

josif

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Feb 03, 2012 at 04:17 AM

You can use Physics.Linecast(enemy, target, hit) and check if the item hit is the target (code in the enemy script):

var target: Transform; // the player transform
...
  var hit: RaycastHit;
  Physics.Linecast(transform.position, target.position, hit);
  if (hit.transform != target){
    // something is in between
  }
...
Comment
Add comment · Show 5 · 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 venkspower · Feb 03, 2012 at 05:56 AM 1
Share

Hi, Aldo.

Can't we use -- transform.LookAt(new Vector3(player.transform.position.x,transform.position.y,player.transform.position.z)); for the enemy to look at the player?

avatar image fafase · Feb 03, 2012 at 06:25 AM 1
Share

Using LookAt will make the NPC look at you (obviously)at all time, even if you are on the other side of the map. You could place a If statement to check distance and if you are more than x, the NPC looks away. But if something is in the way, it will still look at you.jo90 wants it in a way that if you are behind a wall, the NPC is doing something else because it does not see you. Linecast does the job.

avatar image jo90 · Feb 03, 2012 at 07:16 AM 0
Share

thanks for the reply but my script is written in C sharp :S

avatar image aldonaletto · Feb 03, 2012 at 01:33 PM 0
Share

It's almost the same thing - only the variable declarations are different, and an out keyword is needed in Linecast:

public Transform target; // the player transform
...
  RaycastHit hit;
  Physics.Linecast(transform.position, target.position, out hit);
  if (hit.transform != target){
    // something is in between
  }
...
avatar image aldonaletto · Feb 03, 2012 at 09:43 PM 0
Share

@venkspower, I forgot to mention the "look at" thing in my answer - as @fafase said, the code only tells whether the player is visible or hidden behind something. But yours is a good way to implement a LookAt function without tilting the enemy when the player is at a different height - may be useful for @jo90

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

7 People are following this question.

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

Related Questions

How can I more optimize My AI Script 0 Answers

Unity AI FPS problem 0 Answers

Heatmap for tracking enemy threat. 0 Answers

Simple AI Question 1 Answer

Enemy to move out of way of player 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