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 Wuzseen · Dec 19, 2012 at 09:42 PM · raycasttransformlookat

Transform.LookAt Seems To Cause Issues

Hi all, I'm making a "following" script for an object to follow the player if they are within range/sight.

 bool canSeePlayer () {
     RaycastHit hit;
     Vector3 rayDirection = player.position - transform.position;
     float distanceToPlayer = Vector3.Distance( player.position, transform.position );
     if(Physics.Raycast( transform.position,  rayDirection, out hit)) {
         if((hit.transform.tag == "Player") && (distanceToPlayer <= rearDetectionThreshold)){
             //Debug.Log("PLAYER GOT TOO CLOSE");    
             return true;
         }
     }
     if(Vector3.Angle(rayDirection, transform.forward) < fieldOfViewRange ) {
         if(Physics.Raycast( transform.position,  rayDirection, out hit)) {
         //Debug.Log (hit.transform.tag + " " + distanceToPlayer.ToString());
             if((hit.transform.tag == "Player") && (distanceToPlayer < frontDetectionThreshold)){
                 //Debug.Log("CAN SEE PLAYER");    
                 return true;
             }
             else {
                 //Debug.Log("CAN'T SEE PLAYER");
                 return false;
             }
         }
     }
     
     return false;
 }
 
 void Update () {
     if( canSeePlayer() ) {
         //Debug.Log ("MOVE WOLF MOVE");
         transform.LookAt(player);
         transform.position = Vector3.MoveTowards(transform.position, player.position, .1f);
     }
 }

Should be pretty self-explanatory what the code does, uses rays and distances to determine if the player is seen or in range by the object the script is attached to (a cube in this case). It works just as intended, except when I use the transform.LookAt(player) line.

What happens then is that all of the sudden it stops being able to see the player after it changes its direction via lookat. So I'm just not sure what's happening. My line where I print the tag the ray is supposed to be hitting, I get untagged. Which is curious, my player character is definitely tagged as "Player" Again, no issues with the detection when I don't have the cube adjust its direction.

I don't know why it would be inconsistent.

Comment
Add comment · Show 2
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 dannyskim · Dec 19, 2012 at 10:57 PM 0
Share

try using Debug.DrawRay() to ensure your rayDirection is correct.

avatar image Wuzseen · Dec 19, 2012 at 11:00 PM 0
Share

That's a useful thing; unfortunately it seems that the ray is "hitting" the player.

 Debug.DrawRay(transform.position, rayDirection);

Is what you had in $$anonymous$$d right?

It draws a white line between my cube and player

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Wuzseen · Dec 19, 2012 at 11:18 PM

So, in what amounts to basically the silliest thing ever, I had a small sphere as a child of my cube so I could tell what was the "front" of the object. This sphere had a collider on it, which messed up the Raycast.

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

10 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

Related Questions

Have An Object Rotate Around Transform A So That Transform B is Looking at Transform C (C#) 0 Answers

Object moves toward another object when close 2 Answers

How to make AI look at player 0 Answers

Rigidbody to follow player around 1 Answer

Move object along ray cast using object's AddForce or velocity but not transform 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