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 DonReptar · Mar 02, 2015 at 11:09 AM · collisionphysicsartificial intelligence

Collision Detection Between Instances of Prefabs doesn't work

I have a problem, that may have a simple solution. I'm making a 2D game and I have enemies that spawn and follow a player. Everything works fine, except that when 2 enemies should "repel" each other they just overlap.

I have a script with the AI and I'm manipulating the rigidbodies to move the enemies because I read that it should fix the problem, but it didn't.

Here is the AI code

 using UnityEngine;
 using System.Collections;
 
 public class StFleetAI : MonoBehaviour {
 
     Transform target;
     public float rotationSpeed; 
     Transform myTransform; 
     
     void Start()
     {
         myTransform = transform;
         
     }
     
     void RotateToPlayer()
     {
             float x = target.position.x - myTransform.position.x;
             float y = target.position.y - myTransform.position.y;
             float angle = Mathf.Atan2(x, y) * Mathf.Rad2Deg;
             Quaternion q = Quaternion.Euler(0,0,-angle);
             myTransform.rotation = Quaternion.Slerp(myTransform.rotation,q, rotationSpeed*Time.deltaTime);
     }
     
     void Update () {
         target = GameObject.FindWithTag("Player").transform;
         RotateToPlayer();
         rigidbody2D.AddRelativeForce(new Vector2(0,2500));
         restrictMovement();
         
     }
     
     void restrictMovement()
     {
         if (transform.position.x <= -830) {
             transform.position = new Vector3(-830, transform.position.y, transform.position.z);
         } else if (transform.position.x >= 830) {
             transform.position = new Vector3(830, transform.position.y, transform.position.z);
         }
         
         if (transform.position.y <= -535) {
             transform.position = new Vector3(transform.position.x, -535, transform.position.z);
         } else if (transform.position.y >= 535) {
             transform.position = new Vector3(transform.position.x, 535, transform.position.z);
         }
     }
     
 }

Here is the enemy's setup

alt text

I'm not sure if the physics engine works the collisions automatically or if I should use a separate algorithm.

Any help would be awesome, thanks!

captura-de-pantalla-2015-03-01-a-las-153151.png (53.1 kB)
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 tebandesade · Mar 04, 2015 at 09:30 PM 0
Share

I don't know if this will work but maybe your moving your objects with a translate function, which means that it will overwrite the physics engine, therefore it will look messy.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by d2 · Mar 04, 2015 at 09:47 PM

The isTrigger has to be unchecked.

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 d2 · Mar 04, 2015 at 09:48 PM 0
Share

i also recommend you, to use FixedUpdate is$$anonymous$$d of update for the physics

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Keeping player stationary and let physics affect... 0 Answers

Continuous collision detection - unnatural behaviour 0 Answers

Handling colliders on hundreds of asteroids (not procedural asteroids) ...URGENT 3 Answers

Can I change the order that collision layers collide with each other? 1 Answer

Is there anyway to see if another Object has any Collision? 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