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 Kek_Kek · Nov 24, 2017 at 06:22 PM · airaycastingraycasthit

Help with moving object towards player using Raycasting

I made two scripts. 1 for the player and 1 for the AI. The player is supposed to look at the AI and that would cause the AI to move towards the player. When I test it out, it doesn't work. Player Script {

     Ray thugRay;
     RaycastHit rayHit;
     public int dist = 20;
     void Update()
     {
         thugRay = new Ray(transform.position, transform.forward * dist);
         Debug.DrawRay(transform.position, transform.forward * dist, Color.red);
 
         if (Physics.Raycast(transform.position, transform.forward, out rayHit, dist))
         {
             {
                 if (rayHit.transform.gameObject.tag == ("It"))
                 {
                     Debug.Log("He looked at you");
                     GameObject.Find("Control").GetComponent("Move").enabled = false;
 
 
                 }
             }
         }
     }
 }
 

AI Script {

     public Transform player;
     public float walkingDistance = 10.0f;
     public float smoothTime = 10.0f;
     private Vector3 smoothVelocity = Vector3.zero;
 
     RaycastHit rayHit;
 
     private void Start()
     {
         
     }
 
     void HitByRay()
 
     {
 
         Debug.Log("Rip");
         
         transform.LookAt(player);
         float distance = Vector3.Distance(transform.position, player.position);
         if (distance < walkingDistance)
         {
             transform.position = Vector3.SmoothDamp(transform.position, player.position, ref smoothVelocity, smoothTime);
         }
        
 
         rayHit.transform.SendMessage("HitByRay");
 
     }
 }
 
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 Glurth · Nov 24, 2017 at 06:26 PM 0
Share

Objects must have a collider to be detected by Physics.Raycast. Can you confirm your objects have that component?

avatar image Kek_Kek Glurth · Nov 24, 2017 at 06:41 PM 0
Share

Both the AI and the player have a trigger collider.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Legend_Bacon · Nov 24, 2017 at 07:08 PM

Hello there,

could you give us more information on what works and what doesn't please? Which Debug logs get triggered? Does your Debug Ray point towards the right direction?

Also, you say that you want the AI to move towards the player when looked at. But if the ray hits, you seem to be disabling the move component. Unless you are disabling the move component on the player, in which case it's fine.

And finally, when you do see the AI, nothing tells it to do anything. You probably want to have your scripts like that instead:

     Ray thugRay;
     RaycastHit rayHit;
     public int dist = 20;
     private bool iAmDead = false;
     void Update()
     {
         if (iAmDead == false)
         {
             thugRay = new Ray(transform.position, transform.forward * dist);
             Debug.DrawRay(transform.position, transform.forward * dist, Color.red);
 
             if (Physics.Raycast(transform.position, transform.forward, out rayHit, dist))
             {
                 if (rayHit.transform.gameObject.tag == ("It"))
                 {
                     Debug.Log("He looked at you");
                     GameObject.Find("Control").GetComponent<Move>().enabled = false;
                     rayHit.collider.gameObject.GetComponent<AI>().HitByRay();
                     iAmDead = true;
                 }
             }
         }
     }




And now for the AI:

  public Transform player;
     public float walkingDistance = 10.0f;
     public float smoothTime = 10.0f;
     private Vector3 smoothVelocity = Vector3.zero;
 
     RaycastHit rayHit;
 
     private void Start()
     {
 
     }
 
     public void HitByRay()
     {
 
         Debug.Log("Rip");
 
         transform.LookAt(player);
         float distance = Vector3.Distance(transform.position, player.position);
         if (distance < walkingDistance)
         {
             transform.position = Vector3.SmoothDamp(transform.position, player.position, ref smoothVelocity, smoothTime);
         }
 
 
         //rayHit.transform.SendMessage("HitByRay");
 
     }

I hope that helps!

~LegendBacon

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 Kek_Kek · Nov 24, 2017 at 07:21 PM 0
Share

I added the scripts and I tested them out however nothing happens to the AI. No debugs popped up, the debug ray was pointing in the right direction, and the disable move script I added for the player didn't work.

avatar image Legend_Bacon Kek_Kek · Nov 24, 2017 at 07:33 PM 0
Share

On the player script, you are creating a ray "thugRay" that you never use. Try replacing your if (Physics.Raycast(transform.position, transform.forward, out rayHit, dist)) with (if(Physics.Raycast(thugRay, out rayHit, 100))).

Let me know if that changes anything.

~LegendBacon

avatar image Kek_Kek Legend_Bacon · Nov 24, 2017 at 07:59 PM 0
Share

Still does not work. I get these two warnings though.

Field `RaycastingHit.rayHit' is never assigned to, and will always have its default value

NullReferenceException: Object reference not set to an instance of an object Raycasting.Update ()

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

124 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 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 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 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 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 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

Problem with Unity Ray Trace AI Detect Player 0 Answers

Another Raycast Issue. 0 Answers

Getting a point on ray 1 Answer

Can you figure out raycast origin position from RacyastHit? 2 Answers

Layer Mask Detection 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