• 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 DabigMan6 · yesterday · raycast

when my raycast hits the enemies freeze

I'm making a top down shooter and I'm using Ray cast to be fired from the enemy to the player but when I test the game and I get in the range of the enemy to either attack or chase me they just freeze up. and if I'm completely out of their range they walk around like they should. This is my first time using ray casts so I'm not sure if I did something wrong. here is my code:

     Vector3 startRay = transform.position;
     Vector3 rayDirection = player.transform.position;
     RaycastHit hit;

     //Check for sight and attack range
     playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
     playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);

     if (!playerInSightRange && !playerInAttackRange)
     {
         Patroling();
     }
     if (playerInSightRange && !playerInAttackRange && Physics.Raycast(startRay, rayDirection, out hit, sightRange))
     {
         if (hit.collider.tag == "Player")
         {
             ChasePlayer();
         }
     }
     if (playerInAttackRange && playerInSightRange && Physics.Raycast(startRay, rayDirection, out hit, attackRange))
     {
         if (hit.collider.tag == "Player")
         {
             AttackPlayer();
         }
     }


My ChasePlayer function:

private void ChasePlayer() { agent.SetDestination(player.position); }

my AttackPlayer function:

private void AttackPlayer() { agent.SetDestination(transform.position);

     transform.LookAt(player);

     if (!alreadyAttacked)
     {
         Instantiate(projectile, transform.Find("BulletSpawn").position, 
         transform.Find("BulletSpawn").rotation);


         alreadyAttacked = true;
         Invoke(nameof(ResetAttack), timeBetweenAttacks);
     }
 }
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 MarekRimal · 15 hours ago

Hi, the Raycast will probably never hit the player. The second parameter to the Physics.Raycast is direction not destination. Direction is a vector pointing from the origin in the direction of the destination. So change it to Vector3 rayDirection = player.transform.position - transform.position; Now it should hit the player if there is no obstacles between the enemy and the player. So the freezing as you see is probably just standing still because the Raycast never hits.


Recommendation: Its better to avoid expensive method calls such as Physics.CheckSphere or Physics.Raycast whenever you can. You can check the distance directly without using Physics.CheckSphere by playerInAttackRange = Vector3.Distance(transform.position, player.transform.position) < attackRange. Its much faster. The Physics.Raycast is probably necessary here just Physics.Linecast can be little bit faster. Actually Physics.Linecast has second parameter destination so you can just change it to Physics.Linecast instead of Physics.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

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

How to detect if NavAgent has reached hit.point? 3 Answers

Raycast/Melee Attack Help Please! 1 Answer

Identifier expected on equals symbol 1 Answer

List of objects in line of sight - most optimized way to write it? 0 Answers

Raycast Shoot Error 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