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 bloodruns4ever · Apr 28, 2014 at 08:44 PM · topdown

Unity 2D, enemies get stuck along wall

I am currently making a game in Unity 2D, the game is a player in a square arena that has to survive while enemies (green circles) spawn randomly in the arena.

     void Start()
     {
         int randomX = Random.Range(-4, 4);
         int randomY = Random.Range(-4, 4);
         rigidbody2D.velocity = new Vector2(randomX, randomY);
 
     }

So when the enemies spawn, this code makes them shoot off in a random direction with a random speed. My first question is if there is anyway to keep it from spawning with a vector of (0,0), while very rare, totally possible.

My second, more important question, is that when the enemies are bouncing around sometimes they get stuck in a loop against the walls like in the picture.alt text

That circled enemy was stuck going straight up and down along the wall, although it did not start out like that. How would I go about preventing that from happening? Thanks guys!

Here is my enemy Script:

     void Start()
     {
         int randomX = Random.Range(-4, 4);
         int randomY = Random.Range(-4, 4);
         rigidbody2D.velocity = new Vector2(randomX, randomY);
 
     }


and it's spawning code:

 IEnumerator Spawn()
         {
             //Vector3 distanceY = new Vector3(transform.position.y, player.position.y, 0);
     
     
             yield return new WaitForSeconds(startWait);
             for (int i = 0; i < enemyCount; i++)
             {
                 Vector3 spawnLocation = new Vector3(Random.Range(-3, 3), Random.Range(-3, 3), 0);
                 float distanceX = spawnLocation.x - player.position.x;
                 float distanceY = spawnLocation.y - player.position.y;
                 if (Mathf.Abs(distanceX) > 1.5 && Mathf.Abs(distanceY) > 1.5)
                 {
                     Instantiate(enemy, spawnLocation, Quaternion.identity);
                     yield return new WaitForSeconds(spawnWait);
                 }
                 else
                 {
                     i--;
                 }
             }
         }
 

alt text

attributes.png (61.8 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

2 Replies

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

Answer by shopguy · Apr 29, 2014 at 12:24 AM

Your picture is missing so hard to say on the second part.

For the first part if you want to change the spawn point you need to set location, not velocity. I assume you meant you want to avoid 0,0 as the velocity? Why not just add a check like:

if(randomX == 0 && randomY == 0) randomX = 1;

It makes your odds of 0,1 slightly higher than other combos, but does that really matter? If so, I'll post a slightly harder to follow answer.

Comment
Add comment · Show 3 · 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 bloodruns4ever · Apr 29, 2014 at 01:25 AM 0
Share

$$anonymous$$y picture is missing? That's strange, it appears there for me. Also thank you for the answer about the first part, I wouldn't $$anonymous$$d seeing the harder to follow answer though :p

avatar image Zachary Hoffman · Apr 29, 2014 at 01:52 AM 0
Share

@agobloodruns4ever Its missing for me too

avatar image bloodruns4ever · Apr 29, 2014 at 02:06 AM 0
Share

Ahhh hmmm strange... Next time I hop on my laptop I'll try and fix that. Can you see the other image?

avatar image
0

Answer by SteelArrow21 · Apr 29, 2014 at 12:16 AM

For the spawning, after making a random spawning point, do an if statement to see if the position is 0, 0. If it is, do a while statement to keep recreating the coordinates until they both don't equal 0. I can't really help with the bouncing off walls unless I see the script you use.

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 bloodruns4ever · Apr 29, 2014 at 01:28 AM 0
Share

I've added the enemy behavior script as well as the spawning script to the question. That's the only code I have related to the enemy. I also included an image of my enemy prefab attributes

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

23 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

Related Questions

How to create an infinite scrolling background in top down multi-directional shooters. 2 Answers

2D Top Down Game, 2D or 3D defaults? 1 Answer

Unity2d Top Down: Can't move character with Animator enabled 0 Answers

trouble setting map boundaries of top down 2d shooter 1 Answer

top down camara 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