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 /
This question was closed Jan 24, 2018 at 09:48 AM by Alberto69 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Alberto69 · Jan 09, 2018 at 10:51 AM · unity 52d gamecontroltop down shooterenemys

Top down 2d shooter enemy control

In my top down 2d game I created an int variable called dir I assigned him a random number between 0 and 3 I made a switch case where if it is 0 the enemy goes straight if 1 the enemy goes diagonally left if it is 2 the enemy goes right diagonal

this is the part of the code but it does not work

// move the enemy

     Vector2 pos = transform.position;
     Vector2 velocity;

     int dir = (int)Random.Range(0, 3);
     Debug.Log(dir);

     switch (dir)
     {
         case 0:
             velocity = new Vector2(0, -speedEnemy * Time.deltaTime);
             this.transform.Translate(velocity, Space.World);
             Debug.Log("DRITTO");
             break;

         case 1:
             velocity = new Vector2(-speedEnemy * Time.deltaTime, -speedEnemy * Time.deltaTime);
             this.transform.Translate(velocity, Space.World);
             Debug.Log("SINISTRA");
             break;

         case 2:
             velocity = new Vector2(speedEnemy * Time.deltaTime, -speedEnemy * Time.deltaTime);
             this.transform.Translate(velocity, Space.World);
             Debug.Log("DESTRA");
             break;
     }

though the switch case works regularly the enemy always goes left diagonally and not with a smooth movement

my goal is to create random the direction of the enemies

because? what am I doing wrong?

Many thanks for your help

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 Hunterb541 · Jan 09, 2018 at 11:03 AM 0
Share

try making the dir public so you can see if it is changing numbers(0-3) and if it isn't than you found your issue.

2 Replies

  • Sort: 
avatar image
0

Answer by dev-waqas · Jan 09, 2018 at 11:29 AM

You have to Update the position based upon current position of Object. So your code will become.

      Vector2 pos = transform.position;
      Vector2 velocity;
      int dir = (int)Random.Range(0, 3);
      Debug.Log(dir);
      switch (dir)
      {
          case 0:
              // Use current value as starting 
              velocity = new Vector2( this.transform.position.x + 0, this.transform.position.y -speedEnemy * Time.deltaTime);
              this.transform.Translate(velocity, Space.World);
              Debug.Log("DRITTO");
              break;
          case 1:
              velocity = new Vector2(this.transform.position.x-speedEnemy * Time.deltaTime, this.transform.position.y -speedEnemy * Time.deltaTime);
              this.transform.Translate(velocity, Space.World);
              Debug.Log("SINISTRA");
              break;
          case 2:
              velocity = new Vector2( this.transform.position.x+speedEnemy * Time.deltaTime, this.transform.position.y -speedEnemy * Time.deltaTime);
              this.transform.Translate(velocity, Space.World);
              Debug.Log("DESTRA");
              break;
      }
Comment
Add comment · 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
0

Answer by Alberto69 · Jan 09, 2018 at 01:58 PM

hi @dev-waqas,

many tanks i try but now thr code give the follow error

transform.position assign attempt for 'cell_white(Clone)' is not valid. Input position is { 153413570376001570000000000000000000000.000000, Infinity, 0.000000 }. UnityEngine.Transform:Translate(Vector3, Space) EnemyControl:Update() (at Assets/Scripts/EnemyControl.cs:40)

I have 4 enemy cell_white, cell_red,cell_black,cell_yellow

this is the code that i use for eneny spawn random

 public GameObject[] EnemiesPrefab;
  

 float maxSpawnRateInSecond = 5f;

 // Use this for initialization
 void Start () {

    
 }
 
 // Update is called once per frame
 void Update () {
     
 }

 void SpawnEnemy()
 {
     // Botton left point of the screen
     Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));

     // Top right point of the screen
     Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));

     int randEnemy;
     randEnemy = Random.Range(0, EnemiesPrefab.Length);

     // Instantiate an enemy
     GameObject anEnemy = (GameObject)Instantiate(EnemiesPrefab[randEnemy]);
     anEnemy.transform.position = new Vector2(Random.Range(min.x, max.x), max.y);

     // Schedule when spawn next enemy
     ScheduleNextEnemySpawn();
 }

 // Schedule when spawn next enemy
 void ScheduleNextEnemySpawn()
 {
     float spawnInSeconds;

     if (maxSpawnRateInSecond > 1f)
     {
         // Pixk a number between 1 and maxSpawnRateInSecond
         spawnInSeconds = Random.Range(1f, maxSpawnRateInSecond);
     }
     else
         spawnInSeconds = 1f;
     Invoke("SpawnEnemy", spawnInSeconds);
 }

 // Method to increase the difficult of the game
 void IncreaseSpawnRate()
 {
     if (maxSpawnRateInSecond > 1f)
         maxSpawnRateInSecond--;
     if (maxSpawnRateInSecond == 1f)
         CancelInvoke("IncreaseSpawnRate");
 }

 // Method to start enemy spawner
 public void ScheduleEnemySpawner()
 {
     Invoke("SpawnEnemy", maxSpawnRateInSecond);

     // Increase spawn rate every 30 seconds
     InvokeRepeating("IncreaseSpawnRate", 0f, 30f);
 }

 // Method to stop enemy spawner
 public void UnscheduleEnemySpawner()
 {
     CancelInvoke("SpawnEnemy");
     CancelInvoke("IncreaseSpawnRate");
 }

many thanks

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

156 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Firing with the mouse 2D. 3 Answers

Game over funcition isn't being called accurately? 2 Answers

How do I know that an object is not coliding without using OnCollisionExit? 1 Answer

Unity 5.4.0f3 Personal - UI not updating after Build. 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