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 WolfM1nd · Feb 17, 2021 at 04:23 PM · collisioncollider2d-platformerpathfollowing

How to prevent non-collider-objects from touching each other?

Hi, I created a game where ghosts are following the player. There are certain points where these ghosts are spawned. But, after a few seconds, they get into each other or combine with each other because there is only one path that follows the player. I thought preventing them to do that would solve the problem, so I thought of adding colliders, but that would also make them collide with other objects, which I don't want. What should I do? Here is my code: For the Ghosts:

 public class Ghost : MonoBehaviour
 {
 
     public float speed;
     public float StoppingDistance;
     public float RetreatDistance;
     public float idleDistance;
     private Transform TargetPlayer;
 
     private float TimebetweenShots;
     public float StartTimebetweenShots;
 
     public GameObject Projectile;
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         TargetPlayer = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
         TimebetweenShots = StartTimebetweenShots;
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (transform.position.x > TargetPlayer.position.x)
         {
             transform.eulerAngles = new Vector3(0, 180, 0);
         }
         if (transform.position.x < TargetPlayer.position.x)
         {
             transform.eulerAngles = new Vector3(0, 0, 0);
         }
         if (Vector2.Distance(transform.position, TargetPlayer.position) > StoppingDistance)
         {
             transform.position = Vector2.MoveTowards(transform.position, TargetPlayer.position, speed * Time.deltaTime);
         } else if (Vector2.Distance(transform.position, TargetPlayer.position) < StoppingDistance && Vector2.Distance(transform.position, TargetPlayer.position) > RetreatDistance)
         {
             transform.position = this.transform.position;
         } else if (Vector2.Distance(transform.position, TargetPlayer.position) < RetreatDistance)
         {
             transform.position = Vector2.MoveTowards(transform.position, TargetPlayer.position, -speed * Time.deltaTime);
 
         }
         if (TimebetweenShots <= 0)
         {
             Instantiate(Projectile, transform.position, transform.rotation);
             TimebetweenShots = StartTimebetweenShots;
         }
         else
         {
             TimebetweenShots -= Time.deltaTime;
         }
     }
 }
 

And this is the code for spawning if anyone needs it:





 public class GhostSpawner : MonoBehaviour
 {
     public float startTimeBetweenSpawns;
     private float TimebetweenSpawns;
     public float SpawnDistance;
     public GameObject Ghosts;
 
     // Start is called before the first frame update
     void Start()
     {
        TimebetweenSpawns = startTimeBetweenSpawns;
        Instantiate(Ghosts, transform.position, transform.rotation);
     }
 
     // Update is called once per frame
     void Update()
     {
 
         if (Vector2.Distance(GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>().transform.position, gameObject.transform.position) <= SpawnDistance)
         {
 
             if (TimebetweenSpawns <= 0)
             {
                 Instantiate(Ghosts, transform.position, transform.rotation);
                 TimebetweenSpawns = startTimeBetweenSpawns;
             }
             else
             {
                 TimebetweenSpawns -= Time.deltaTime;
             }
         }
     }
 }


Also, here is a screenshot from my gameplay. alt text alt text

Here there are actually three ghosts.

screen-shot-2021-02-17-at-104135.png (22.2 kB)
screen-shot-2021-02-17-at-104209.png (15.0 kB)
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
Best Answer

Answer by DevManuel · Feb 18, 2021 at 07:56 AM

Hi, you can change the layer, on which the object are colliding. If you change these layers, they don't collide anymore..

UnityDoc: LayerCollision

Comment
Add comment · Show 6 · 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 WolfM1nd · Feb 18, 2021 at 08:01 AM 0
Share

But, the ghosts are spawned from one prefab. If I change that, won't that alter all the clones?

avatar image WolfM1nd WolfM1nd · Feb 18, 2021 at 08:02 AM 0
Share

I only want them to get separated, otherwise, it seems there is only 1 ghost whereas there actually exists three.

avatar image DevManuel WolfM1nd · Feb 18, 2021 at 08:10 AM 0
Share

You can put all your objects that you want them to ignore each other on the same custom layer. Then in your Physics Manager, just uncheck it to make your objects ignore collisions if both them and the object they are colliding with is on the same custom layer. More info here: link

Show more comments

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

211 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

Related Questions

Stop object colliders (non-mesh) bouncing on collision 1 Answer

Unity2D side collision detection 1 Answer

2D Collisions dont work when built.,Colliders not working once built 1 Answer

How to detect a collision between the character and the bottom of a cube? 1 Answer

How to switch one image with another when walked into? 2 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