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 RjToni · Sep 11, 2012 at 02:19 AM · raycastaicolliders

I need an explanation about raycast and collisions

Hey Guys!

So, I'm trying to script a simple AI routine for the enemy bots for my game.

Thanks to you all, I've managed to make a patrol using waypoints, now, I wanto to make this.

If the player enter in the enemy view, the enemy will pursuit and attack (melee), and if the enemy can't see the player anymore, backs to the patrol.

I believe that, the best way to do this, is using the raycast and colliders, so, can you guys tell me a little more about colliders and raycasts? I mean, the question is:

How can my enemies "see the walls"?

How can I use the colliders to make my enemies check if is a wall or the hero?

How can I use the raycast to make a viewpoint for my enemies?

And, how can I make my hero collide to the enemy's view, and NOT see behind the walls?

I hope don't act confuse (my English doesn't help, though), but I appreciate your kindness, anyway

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
0

Answer by aldonaletto · Sep 11, 2012 at 03:14 AM

The most used way to "see" a target without that annoying X-ray effect is to use Physics.Linecast(enemy.position, target.position), and check if the object hit is the target - if it's not, there's something in between:

function CanSeeTarget(target: Transform): boolean {
  var hit: RaycastHit;
  if (Physics.Linecast(transform.position, target.position, hit)
      && hit.transform == target){
    return true;
  }
  return false;
}
You can also add more restrictions, like limited view range and angle:

var range: float = 100; // viewing range var viewAngle: float = 15; // viewing angle

function CanSeeTarget(target: Transform): boolean { var hit: RaycastHit; if (Physics.Linecast(transform.position, target.position, hit, range) && hit.transform == target && Vector3.Angle(target.position-transform.position, transform.forward) < viewAngle){ return true; } else { return false; } }

Comment
Add comment · Show 3 · 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 RjToni · Sep 11, 2012 at 05:56 AM 0
Share

I've tryed in too many ways, but aways return the same error "Assets/IAEnemy.js(42,17): BCE0017: The best overload for the method 'IAEnemy.CanSeeTarget(UnityEngine.Transform)' is not compatible with the argument list '()'." That's my script so far ( It's a real mess, I know)

var Target : Transform; var PointA : Transform; var PointB : Transform; var range: float = 100; // viewing range var viewAngle: float = 15; // viewing angle

private var NavComponent : Nav$$anonymous$$eshAgent;

function Start () {

NavComponent = this.transform.GetComponent(Nav$$anonymous$$eshAgent);

BeARobot();

}

function CanSeeTarget(Target: Transform): boolean { var hit: RaycastHit; if (Physics.Linecast(transform.position, Target.position, hit, range) && hit.transform == Target && Vector3.Angle(Target.position-transform.position, transform.forward) < viewAngle){ return true; } else { return false; } }

function Update() {

if (CanSeeTarget()) print("I sense the enemy is near!");

    // print("I sense the enemy is far!");    

}

function walktoA () { NavComponent.SetDestination(PointA.position); animation.Play("walk"); } function wait () { animation.Play("idle"); } function walktoB() { NavComponent.SetDestination(PointB.position); animation.Play("walk"); } function seek() { NavComponent.SetDestination(Target.position); animation.Play("run"); }

function BeARobot() {

Invoke( "walktoA", 0.1 ); Invoke("wait", 3.0); Invoke( "walktoB", 10.0 ); Invoke("wait", 13.0); Invoke( "BeARobot", 15.0 ); }

avatar image aldonaletto · Sep 11, 2012 at 06:09 AM 0
Share

You must pass the target transform to the function CanSeeTarget:

var player: Transform; // drag the player here
...
  if (CanSeeTarget(player)){
    print("I can see you, bastard!");
  }
avatar image RjToni · Sep 11, 2012 at 07:18 AM 0
Share

Well, I'm Still having problems. The script is not showing errors anymore, however, The bot, doesn't see the player, And I don't have idea. $$anonymous$$y script so far is like this:

var player : Transform;

var PointA : Transform;

var PointB : Transform;

var range: float = 100; // viewing range

var viewAngle: float = 15; // viewing angle

private var NavComponent : Nav$$anonymous$$eshAgent;

function Start () {

NavComponent = this.transform.GetComponent(Nav$$anonymous$$eshAgent);

BeARobot();

}

function CanSeeTarget(player): boolean {

var hit: RaycastHit;

if (Physics.Linecast(transform.position, player.position, hit, range) && hit.transform == player && Vector3.Angle(player.position-transform.position, transform.forward) < viewAngle){

 return true;

} else {

 return false;

} }

function Update() {

if (CanSeeTarget(player))

   {  print("I can see you, bastard!");
      

} else { print("I'm not working");} // It's just returning that

}

function walktoA () { NavComponent.SetDestination(PointA.position); animation.Play("walk"); } function wait () { animation.Play("idle"); } function walktoB() { NavComponent.SetDestination(PointB.position); animation.Play("walk"); } function seek() { NavComponent.SetDestination(player.position); animation.Play("run"); }

function BeARobot() {

Invoke( "walktoA", 0.1 ); Invoke("wait", 3.0); Invoke( "walktoB", 10.0 ); Invoke("wait", 13.0); Invoke( "BeARobot", 15.0 ); }

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

9 People are following this question.

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

Related Questions

AI Shooting through walls. 1 Answer

Visible GameObjects for AI 0 Answers

Help me with AI 1 Answer

AI Raycast problem 1 Answer

Fire projectile, tracing a Raycast, enemy AI, 2D 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