• 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

AddForce to a point of a GameObject, not the center 1 Answer

Camera gets flung off of the map when the player collides with certain objects. 0 Answers

Punching a crate 0 Answers

Imported 3D models collisions 1 Answer

FPS Tutorial: Why do my grenades go through the walls? 3 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