Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by VironFlo · May 21, 2021 at 09:55 AM · scripting problemcollisiontriggerlayermaskspherecast

Weird result using Spherecast

Hi, I try to make my enemy IA able to dodge but weird result happens : It's like the spherecast ignore the layermask?

Code of IA :

 switch(enemy_state)
         {
             case enemyState.chasing:
                 agent.SetDestination(target.position);
                 break;
             case enemyState.attacking:
                 agent.SetDestination(target.position);
                 lookingPlayer();
                 if(!alreadyAttacked)
                 {
                     Rigidbody rbProjectile = Instantiate(projectileEnemy, transform.position, 
                     Quaternion.identity).GetComponent<Rigidbody>();

                     rbProjectile.velocity = new Vector3(0f, 0f, 0f);
                     rbProjectile.angularVelocity = new Vector3(0f, 0f, 0f);
                     rbProjectile.AddForce(transform.forward * 32f, ForceMode.Impulse);
                     rbProjectile.AddForce(transform.up * -0.8f, ForceMode.Impulse);
                     alreadyAttacked = true;
                     Invoke(nameof(ResetAttack), timeBetweenAttacks);
                 }
                 if(!currentlyDodging)
                 {
                     StartCoroutine("ScanBulletOfPlayer");
                 }
                 break;
             case enemyState.Patroling:
                 if (!walkPoinset)
                     SearchWalkPoint();
                 if (walkPoinset)
                     agent.SetDestination(walkPoint);
                 Vector3 distanceToWalkPoint = transform.position - walkPoint;
 
                 if(distanceToWalkPoint.magnitude<1f)
                 {
                     walkPoinset = false;
                 }
                 break;
              default:
              break;
         }
 private IEnumerator ScanBulletOfPlayer()
     {
         float timefocused = 1.2f;
         Vector3 forwardEnemy = transform.TransformDirection(Vector3.forward * 10);
         Debug.DrawRay(transform.position, forwardEnemy, Color.blue);
         RaycastHit hit;
         int layerMask = 1 << 10;
         float offsetmove = 3f;
         if(Physics.SphereCast(forwardEnemy,4f,transform.forward,out hit, LayerBullet))
         {
             Debug.Log("bullet player hit !");
             transform.position +=Vector3.right* offsetmove;
         }
         yield return new WaitForSeconds(timefocused);
         currentlyDodging = true;
         Invoke(nameof(ResetDodge), timeBetweenDodge);
     }
     private void ResetDodge()
     {
         currentlyDodging = false;
         StartCoroutine("ScanBulletOfPlayer");
     }

PS : transform.position +=Vector3.right* offsetmove; is just for testing

Here is the result : Video of result

Comment
Add comment · Show 1
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 unity_662CE877CA6B90E20ED4 · May 21, 2021 at 10:14 AM 0
Share

Hello, Did you tried to Debugg the SphereCast ? You can do that with Gizmos.DrawSphere (); Hope it help

1 Reply

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

Answer by VironFlo · May 21, 2021 at 02:19 PM

Solved : I delete raycast stuff and remplace by this :

new Coroutine :

 private IEnumerator ScanBulletOfPlayer()
     {
         float timeOfdodging = 0.8f;
         yield return new WaitForSeconds(timeOfdodging);
         currentlyDodging = true;
         Invoke(nameof(ResetDodge), timeBetweenDodge);
     }

the switch (dodging) :

  if(!currentlyDodging)
                 {
                     currentMovedir = Random.Range(-1, 2);
                     Debug.Log(currentMovedir);
                     StartCoroutine("ScanBulletOfPlayer");
                 }
                 else
                 {
                     agent.Move(transform.right * currentMovedir);
                 }

Very clean result + no predictable + easier :)

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

281 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

2D Platformer - Picking Up Items & Storing Them C# 0 Answers

Collison detection not working? 1 Answer

Trigger is not detecting Player. (2D) 1 Answer

Collision without movement!?? 2 Answers

Activate trigger if items colected 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