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 /
avatar image
0
Question by zmar0519 · Jun 15, 2011 at 10:23 AM · aishootinglinecast

Can See Target Not Working

Hello, everybody. I've got a problem with my AI Script. Although I want it to check if there is something in the way of the player and thereby give up pursuit/shooting at him, CannotSeeTarget() : boolean never returns false. I was hoping that there would be a way to fix this. All help would be appreciated!!! :)

Here is my Code:

 enum s
 {
     Idle,
     Pursuit,
     Shooting
 }    
 var Player : Transform;
 var walkSpeed : float;
 var runSpeed : float;
 var obstacleAvoidanceLevel : float;
 var patrolChangesInterval : float;
 var shootRange : float;
 var noticeRange : float;
 var showGizmosInEditor : boolean;
 var avoidEdges : boolean;
 var gun : AIGun;
 
 var Controller : CharacterController;
 
 @HideInInspector
 var state : s;
 
 private var lastChange : float;
 private var moveSpeed : float;
 function Awake()
 {
     patrolChangesInterval += Random.Range(-1.5, 1.5);
     moveSpeed = walkSpeed;
 }    
 function Update()
 {
     Debug.Log(CannotSeeTarget());
     Controller.Move(-Vector3.up * Time.deltaTime * 10);
     Controller.Move(transform.forward * moveSpeed*Time.deltaTime);
     if(Time.time > lastChange)
     {
         lastChange = Time.time + patrolChangesInterval;
         ChangeDirection();
     }
     
     var hit : RaycastHit;
     if(Physics.Raycast(transform.position, transform.forward, hit, obstacleAvoidanceLevel))
     {
         if(hit.collider.tag != "Player")
         {
             ChangeDirection();
         }
     }
     var dist : float;
     dist = Vector3.Distance(Player.transform.position, transform.position);
     if(dist < noticeRange && dist > shootRange && !CannotSeeTarget())
     {
         state = s.Pursuit;
         moveSpeed = runSpeed;
     } else if(dist < shootRange && !CannotSeeTarget())
     {
         state = s.Shooting;
         moveSpeed = 0;
     } else {
         state = s.Idle;
         moveSpeed = walkSpeed;
     }
     if(state != s.Idle)
     {
         transform.LookAt(Player.transform);
         if(state == s.Shooting)
         {
             gun.Shoot(transform.forward, transform.rotation);
         }
     }
         
     
 }
 function ChangeDirection ()
 {
     transform.Rotate(Vector3(0, Random.Range(90, 270), 0));
 }
 function CannotSeeTarget() : boolean
 {
     var hit : RaycastHit;
     return Physics.Linecast(transform.position, Player.position, hit);
     if(hit != null)
         Debug.Log(hit.collider.name);
 }
 function OnDrawGizmosSelected () 
 {
     if(showGizmosInEditor)
     {
         Gizmos.color = Color.yellow;
         Gizmos.DrawWireSphere(transform.position, noticeRange);
         Gizmos.color = Color.red;
         Gizmos.DrawWireSphere(transform.position, shootRange);
     }
 }
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 PaulUsul · Jun 16, 2011 at 01:03 PM 0
Share

You should really rename that method to CanSeeTarget, would greatly improve the readability. Reading through it now, hope i can help

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PaulUsul · Jun 16, 2011 at 01:32 PM

If CannotSeeTarget never returns false, it is because there is something in the way of your linecast. This could be because it is linecasting from the pivot point of your enemy to the players pivot point and they are not where you thought they'd be. No matter the reason you need to find out what and where it goes wrong, so save the point where it hits and Gizmos.DrawLine from the 2 points should reveal something.

Hope this helps :)

 var HitPosition : Vector3;
 function CannotSeeTarget() : boolean
 {
     var hit : RaycastHit;
     if(Physics.Linecast(transform.position, Player.position, hit))
     {
         HitPosition = hit.point;
         return true;
     }
     else
     {
         return false;
     }
     if(hit != null)
         Debug.Log(hit.collider.name);
 }
 function OnDrawGizmosSelected () 
 {
     if(showGizmosInEditor)
     {
         Gizmos.color = Color.yellow;
         Gizmos.DrawWireSphere(transform.position, noticeRange);
         Gizmos.color = Color.red;
         Gizmos.DrawWireSphere(transform.position, shootRange);
         Gizmos.color = Color.blue;
         Gizmos.DrawLine(transform.position, Player.position);
         Gizmos.color = Color.green;
         Gizmos.DrawWireSphere(HitPosition , 1);
     }
 }
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

2 People are following this question.

avatar image avatar image

Related Questions

Can See Target not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Shooting AI problem 2 Answers

Coordinating multiple AI enemies 3 Answers

How to make basic AI in a 2d game? 4 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