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 jordmax12 · Mar 23, 2014 at 05:13 PM · c#raycast

Trying to get my AI to turn and face direction they are going

Can't seem to figure out why I am having such a hard time at getting my AI to look in direction they are moving. It's hard to show this in code, but basically I have a flocking state machine for my AI and everything is great. I have raycasts to detect if they see or hit a wall, except the raycast is always facing the same spot. If my AI is moving in opposite direction, the raycast is still 180 degrees in the wrong direction. Wondering if anyone knew any solutions. Again, I need my cast to always point in the direction that my AI is going. I would post code, but I really don't know how that would help but here is my AI class just in case it's useful to someone. Any help is appreciated thanks in advance.

Function I am calling the raycast in

 void MessageOtherAI(Vector3 tempTrans1, Vector3 tempTrans2, float tempFloat)
     {
         RaycastHit hit;
 
             //sees wall
         if (Physics.Raycast(tempTrans1, tempTrans2, out hit, tempFloat))
         {
             Debug.Log ("I see a wall!");
             gameObject.renderer.material.color = new Color(0, 0, 1, 1); //blue
             Debug.DrawRay (transform.position, directionOfRay * Range, Color.blue);
 
             if(tempFloat < 5f)
             {
                 Debug.Log ("I have hit a wall!");
                 gameObject.renderer.material.color = new Color(1, 0, 0, 1); //red
                 Debug.DrawRay (transform.position, directionOfRay * Range, Color.red);
 
                 //essentially this sets hitWall to true
             }
         }
 
     }

Steer function

 protected virtual Vector3 Steer(Vector3 target, bool slowDown)
     {
         // the steering vector
         Vector3 steer = Vector3.zero;
         Vector3 targetDirection = target - transform.position;
         float targetDistance = targetDirection.magnitude;
         
         transform.LookAt(target);
         
         if (targetDistance > 0)
         {
             // move towards the target
             targetDirection.Normalize();
             
             // we have two options for speed
             if (slowDown && targetDistance < 100f * speed)
             {
                 targetDirection *= (maxSpeed * targetDistance / (100f * speed));
                 targetDirection *= speed;
             }
             else
             {
                 targetDirection *= maxSpeed;
             }
             
             // set steering vector
             steer = targetDirection - rigidbody.velocity;
             steer = Limit(steer, maxSteer);
         }
         
         return steer;
     }

Search function

 protected virtual void VSearchState()
     {
         seesWall = false;
         hitWall = false;
         Transform wall = GameObject.FindWithTag ("SolidObject").transform;
 
         if (movementFlock == true)
         {
 
             RaycastHit hit;
             
             Vector3 directionOfRay = transform.TransformDirection(Vector3.forward);
             
             //Debug.DrawRay (transform.position, directionOfRay * Range, Color.red);
 
             if (Physics.Raycast(transform.position, directionOfRay, out hit, Range))
             {
                 
                 if(hit.collider.gameObject.CompareTag("SolidObject"))
                 {
                     seesWall = true;
                     //Debug.DrawRay (transform.position, directionOfRay * Range, Color.green);
                     MessageOtherAI (transform.position, directionOfRay, Range); //if position is within the raycast
 
                     if (Physics.Raycast(transform.position, directionOfRay, out hit, hitWallRange))
                     {
                         hitWall = true;
                         MessageOtherAI (transform.position, directionOfRay, hitWallRange);
                     }
                     //set to move in a random direction now...
 
                 }
                 
             }
 
             Vector3 detectWallHit = wall.transform.position - transform.position;
             float distanceFromWall = detectWallHit.magnitude;
             if(distanceFromWall < 2f)
             {
                 /*Debug.Log ("TEST");
                 gameObject.renderer.material.color = new Color(0, 0, 1, 1); //blue*/
 
             }
         }
         
     }
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 aubreuil56 · Mar 23, 2014 at 08:00 PM

Need to know when in the Update() loop these transform calculations are happening, are you making use of LateUpdate() or skipping every other frame ? Some objects like transform forward vectors don't update when you expect them to (ie setting transform.LookAt() doesn't actually do anything until the next frame)

Comment
Add comment · Show 1 · 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 jordmax12 · Mar 23, 2014 at 08:12 PM 0
Share

Don't have my thing up but I am making use of "FixedUpdate" where all the calculations are being made. :), FixedUpdate I heard was a lot better for Physics type stuff so that's why I've been using all movement in there.

I think I did figure out that my raycasts where in the direction, but my Debug.DrawRay isn't. That's what's ALWAYS facing one direction, how do I get that to face the direction of the transform it's attached to? (I've tried multiple things so far and none have worked).

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

21 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Cant jump while running left or right c#? 3 Answers

How to select an object with TOUCH and change its animation 2D 1 Answer

how to instantiate an object that is a child of another object 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