• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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 Lo_Mein_Gang · Jan 20 at 05:45 PM · movementprefabbeginnerobjects

Position of all spawned prefabs being effected by MoveTowards (beginner question)

Just want to begin by saying that I am an extreme new comer to Unity and game programming in general. So this question is probably going to be extremely obvious, but it is giving me trouble.

So to try and explain my issue as best as possible. My game im working on is a really simple shoot-em up type game where I have prefabs(enemies) spawning from the right side moving left. A gameplay element I have is that the player can occasionally shoot some hacking shots that will give you control of the enemies and they are basically the usually upgrades you would have in a shoot-em up.

My issue comes up when I shoot the enemies with the hacking shot i want to use the MoveTowards to move them to a designated location (emptyobject) by the player. I have it working so that when i shoot a single enemy on screen it will move to place. However, if there are more than one one screen they all start to move to the designated location even if I have only shot one of the enemies.

I feel like there is some way to only manipulate the position of a single prefab at a time and not change all of them together.

Let me know if there is any other info you could use to help solve my issue.

Thanks!

      public class enemy2 : MonoBehaviour
      {
          public int speed;
      
          private Rigidbody2D rb;
      
          public GameObject enemyBullet2;
      
          private int bulletDelay = 6;
      
      
          private static Transform openSpot;
      
      
          // Movement speed in units/sec.
          public float moveSpeed = 50;
      
          // Time when the movement started.
          private float startTime;
      
          // Total distance between the markers.
          private float journeyLength;
      
          private static bool beginMove = false;
      
          private 
      
      
          // Start is called before the first frame update
          void Start()
          {
              
              rb = GetComponent<Rigidbody2D>();
      
              rb.velocity = Vector2.left * speed;
      
              StartCoroutine(spawnBullet());
      
          }
      
          // Update is called once per frame
          IEnumerator spawnBullet()
          {
      
                  yield return new WaitForSeconds(.5f);
      
      
                  for (int i = 0; i < 4; i++)
                  {
                  while (beginMove == false)
                  {
                      Instantiate(enemyBullet2, transform.position, Quaternion.identity);
                      yield return new WaitForSeconds(1);
                  }
                  }
              
      
          }
          private void Update()
          {
              if (beginMove == true)
              {
      
      
                  rb.velocity = Vector2.left * 0;
                  // Move our position a step closer to the target.
                  gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, openSpot.position, 1);
      
                  if (gameObject.transform.position == openSpot.position)
                  {
                      beginMove = false;
                  }
              }
          }
      
          void OnTriggerEnter2D(Collider2D col)
          {
              if (col.tag == "despawner")
              {
                  Destroy(gameObject);
              }
      
              if(col.tag == "Player" && beginMove == false)
              {
                  Destroy(gameObject);
              }
      
              if (col.tag == "hacker")
              {
                  if (Spaceship.currentSpot < 6) { 
                  
                  beginMove = true;
                  openSpot = Spaceship.nextOpenSpot.transform;
                 }
              }
          }
      
      
      
      }
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
1

Answer by xxmariofer · Jan 20 at 07:44 PM

Hello, even you are spawning them as a prefab, they are all different objects so if each prefab object have that script they should not be affected if only one openSpot position was changed, my guess is that all enemysh are triggering with the hacker bullet, can you put a debug.log inside the ontrigger inside the if (col.tag == hacker) and check how many times is entering when only one of them gets hit?

Comment
Add comment · Show 9 · 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 Lo_Mein_Gang · Jan 20 at 09:00 PM 0
Share

Thanks for the response!

I added the Debug.Log right inside the if(col.tag == hacker) and it is only getting printed once when I shoot a single enemy.

avatar image xxmariofer Lo_Mein_Gang · Jan 20 at 09:17 PM 1
Share

Ok just checked your code, you are using a static var,

just remove the static leave it:

private Transform openSpot;

you can change openSpot to vector3 and refactor a bit for better performance, if you need help understanding static vars i can give you good examples.

avatar image Lo_Mein_Gang xxmariofer · Jan 20 at 09:50 PM 0
Share

That seems to solve part of my issue. Now only the enemy I shoot moves into spot, which is great.However, now the issue is that when I try to shoot the next enemy and want it to move into the next openspot it does not move there.

down in the if(col.tag == hacker) that is where I have added code which I am iterating through an array every time an enemy is hit by a hackerBullet the enemy will fill in a new designated spot.

my new code is:

   if (col.tag == "hacker")
             {
                 Debug.Log("isHit");
                 if (Spaceship.currentSpot < 6)
                 {
     
                     beginMove = true;
                     openSpot = Spaceship.nextOpenSpot.transform;
     
                    
                     Spaceship.incrementOpenSpot();
     
     
     
                 }
             }
 


Edit: upon further inspection I realized that I once again had my beginMove as a static var. I that out and now all the ship are moving fine. However, this brings me to my new issue where it seems like the array, that contains all the empty child objects to my main player that i want to enemies to move, is not being iterated through. So now with every shot all the enemies are moving to the first spot and not the second, third, etc with every additional shot landed.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

189 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

Related Questions

VR - setting up cockpit to drift slightly 0 Answers

Setting game object position on startup? (Beginner) 2 Answers

Help with Basic Movement Script 1 Answer

Moving a non-player object in a random direction which bounces off other objects. 2 Answers

Script stopped working, prefabs behaving strangely 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges