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 Impstar · Oct 24, 2017 at 04:47 AM · unity 5physicsenemy ai

Object bounces off wall, but on return goes through it

I'm playing a game where enemies are coming at your base and you can build walls to hold them off. If an enemy hits your wall, they bounce back. Then they try to go after the wall again until it's destroyed. The first bounceback works fine. Later bounces go straight through the wall. The wall has a collider on it, and so does the enemy. The enemy also has a rigidbody on them. My knockback function is:

    void Update()
 {
     if (noWall)
         rigid.MovePosition(Vector3.MoveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime));
     if (!noWall)
     {
         StartCoroutine(KnockBack());
     }
 }

 private void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Wall")
     {
         noWall = false;
     }
 }

 IEnumerator KnockBack() //causes enemy to be propelled backwards, momentarily stop to end the backwards force, then move forwards again
 {
     Vector3 dir = SelectedTarget.position - transform.position;
     dir = -dir.normalized;
     rigid.AddForce(dir * 10);
     yield return new WaitForSeconds(1f);
     rigid.velocity = new Vector3(0, 0, 0);
     rigid.MovePosition(Vector3.MoveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime)); //this movement goes through my walls
     noWall = true;
 }

I have two types of enemies, large and small. Large enemies do bounce off indefinitely unless they're knocked forwards. Small enemies bounce one, and then go through. I'm not sure why this is happening. Is there someone who can explain this?

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 jchester07 · Oct 24, 2017 at 05:35 AM 0
Share

Just a suggestion on your script. You can basically remove rigid.$$anonymous$$ovePosition(Vector3.$$anonymous$$oveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime)); from your knockback method.

and also just call your $$anonymous$$nockBack method inside the OnTriggerEnter() ins$$anonymous$$d of calling it in the update.

avatar image Impstar jchester07 · Oct 24, 2017 at 05:55 AM 0
Share

Thanks! I made those changes, and it actually fixed my problem. For reasons that I don't really understand.

1 Reply

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

Answer by Impstar · Oct 24, 2017 at 06:00 AM

I got it to work, here's the working version. Minor changes and I'm not sure why it matters, but apparently it does.

    void Update()
 {
     if (noWall)
         rigid.MovePosition(Vector3.MoveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime));
 }

 private void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Wall")
     {
         noWall = false;
         StartCoroutine(KnockBack());
     }
 }

 IEnumerator KnockBack() //causes enemy to be propelled backwards, momentarily stop to end the backwards force, then move forwards again
 {
     Vector3 dir = SelectedTarget.position - transform.position;
     dir = -dir.normalized;
     rigid.AddForce(dir * 10);
     yield return new WaitForSeconds(1f);
     rigid.velocity = new Vector3(0, 0, 0);
     noWall = true;
 }

I moved the StartCoroutine to the OnTriggerEnter function and got rid of the

  rigid.MovePosition(Vector3.MoveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime)) 

down in the Knockback function.

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 jchester07 · Oct 24, 2017 at 09:04 AM 0
Share

It's good to know that fixed your problem. Though logically, your original script should work. $$anonymous$$ust be due to Coroutine. I just learned that many are against it. I don't know why but maybe this is one of the reasons.

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

189 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

Related Questions

Similar movement to a game (look description) 1 Answer

How do you make an wire/rope type object and attach to 2 separate objects 1 Answer

How to apply frictional Torque 2 Answers

Physics.overlapSphere doesnt detect colliders,Physics.CheckSphere doesn't detect colliders 1 Answer

Problem is not Bouncing 0 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