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 Juri · Oct 19, 2010 at 02:08 PM · physicsraycastai

Raycast problems in my AI

hey, I try to make this AI working for quite a while now.. The problem i have with it is, that a the raycast always returns positiv, even if the Object it hits is not my player!! I dont know what I am doing wrong! this is my whole AI... I know that it is not the best jet ;P

var target : Transform; var moveSpeed = 3; var rotationSpeed = 3; var hunt : boolean = false; var myTransform : Transform; var distance : int; var inSight : boolean = false; var runSpeed = 9; function Awake() { myTransform = transform; }

function Start() { target = GameObject.FindWithTag("Player").transform;

}

function Update () {

 dist = (transform.position - target.position).magnitude;
 if(dist < distance){
     hunt = true;
 }
 else{
     hunt = false;
 }
 var hit : RaycastHit;
 var direction : Vector3;
 direction =target.position - transform.position;
 if(Physics.Raycast(transform.position , direction, hit)) {
     if(hit == GameObject.FindWithTag("Player")) {
         var InSight = hit.distance;
     }
 }

 if(InSight <= distance){
     inSight =true;
 }
 else{
     inSight = false;
 }

 if(hunt == true){

 myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
 Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);


 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }
 if(inSight == true){
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
 Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }
 if (Player_Simply.running == true) {
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
 Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 myTransform.position += myTransform.forward * runSpeed * Time.deltaTime;
 }



}

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 skovacs1 · Oct 19, 2010 at 03:56 PM

Physics.Raycast will raycast against every object within the layers specified by the cullingMask which you leave as kDefaultRaycastLayers, not just against just your player as your question states that you are expecting.

  • hit is a RaycastHit, not a GameObject. hit == GameObject.FindWithTag("Player") will always return false. You should probably use hit.collider.gameObject == GameObject.FindWithTag("Player") which will actually work.
  • You declare the InSight variable within the scope of the two if statements and then try to access it in a different scope. This will not work.
  • Why do you even bother with GameObject.FindWithTag("Player") within your raycast if when you store the transform of this GameObject in target?
  • You check a boolean condition to set hunt, but you only use it in one place. Why not just check the condition itself? Isn't it the same with inSight?
  • You calculate target.position - transform.position up to five times - why?
  • You do essentially the same thing in all three if statements. Why?
  • You're not supposed to pass Slerp/Lerp a deltaTime, but an incremental time value unless you want it to always look some small amount towards the target. Since you are also slerp/lerping from a constantly changing rotation to a potentially constantly changing rotation by some tiny control amount, you will never actually reach the target unless your frame rate should slow significantly (in your case to rotationSpeed frames per second).

This does what it seems you are trying to do and should actually accomplish it (Note that the Slerp here is not framerate dependent anymore):

var target : Transform; var huntRange : float = 20.0f; var rotationSpeed : float = 0.1f; var moveSpeed : float = 3.0f; var runSpeed : float = 5.0f; private var speed : float;

function Start() { target = GameObject.FindWithTag("Player").transform; }

function Update() { var hit : RaycastHit; var direction : Vector3 = target.position - transform.position;

 if(direction.magnitude < huntRange
     && (Physics.Raycast(transform.position , direction, hit)
         && hit.collider.gameObject == target.gameObject)) {

     //Shorter than writing 2-3 ifs that do the same thing
     if(Player_Simply.running) speed = runSpeed;
     else speed = moveSpeed;

     transform.rotation = Quaternion.Slerp(transform.rotation,
                                 Quaternion.LookRotation(direction),
                                 rotationSpeed);
     transform.position += transform.forward * speed * Time.deltaTime;
 }

}

Comment
Add comment · Show 7 · 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 Juri · Oct 20, 2010 at 11:30 AM 0
Share

thanks for ur great help, but i tried ur script, and the only thing that my hunter is doing is forward! he is not looking at my player nor is he running after him! is it a problem with the script or is it me??? so basicly the new script is doing less then my old one... but ur it looks a lot better :P (this is not supposed to say, that i don't appreciate ur help)

avatar image skovacs1 · Oct 20, 2010 at 02:03 PM 0
Share

Yeah. Sorry about that. Forgot the transform. before rotation - I'm a little surprised that wasn't an error actually.

avatar image Juri · Oct 20, 2010 at 02:32 PM 0
Share

where is a transform. missing???

avatar image skovacs1 · Oct 20, 2010 at 04:20 PM 0
Share

It was missing. See where it says I edited the post 2 hours ago? That's when I fixed it.

avatar image Juri · Oct 22, 2010 at 09:18 AM 0
Share

ok so now its doing everything that it should, but it is ignoring everything in its way. when i put a box in its way with a collider on it, the hunter is just going through it. maybe u can tell me a way, so i can send u my project by now i think that there is a problem in a other script! thanks

Show more comments

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

No one has followed this question yet.

Related Questions

Physics.Raycast 2 Answers

Physics.Raycast AI problem 1 Answer

C# Check Physics.Raycast Once 0 Answers

How to stop enemy shooting through wall 1 Answer

Raycasting from the sides of a GameObject 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