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 orjn0101 · Nov 13, 2013 at 01:21 AM · gameobjectpositionspawningtranslate

help with gameObject.translate

Hi I'm pretty new to this so it's probably some really obvious mistake that I can't find. I'm making a simple tower defense game and for testing purpose I'm trying to spawn a new enemy every time I'm pressing the space bar. It works and a new clone of my enemy is created every time I press it. Then I tried to make the enemies move by attaching this pathfinder script to them(It is supposed to become one later). My problem is that it moves the spawn point and the enemies spawns on top of each other and moving exactly the same. I want the spawn point to be in the same position and the enemies to spawn there and then start to move. Here are my two scripts. the GameMaster.cs is attached to an empty gameobject and the PathFinding.cs is attached to my enemy prefab.

 using UnityEngine;
 using System.Collections;
 
 public class GameMaster : MonoBehaviour {
     
     public GameObject enemy;
     public static Transform spawnPoint;
     
     // Use this for initialization
     void Start () {
         spawnPoint = GameObject.FindGameObjectWithTag("Spawn Point").transform;
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetKeyDown("space"))
             SpawnEnemies();
     }
     
     public void SpawnEnemies() {
         Debug.Log("Enemies spawned!");
         GameObject.Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);  
     }
 }

 
 using UnityEngine;
 using System.Collections;
 
 public class PathFinding : MonoBehaviour {
     
     public Transform enemyPosition;
     
     // Use this for initialization
     void Start () {
         enemyPosition = GameMaster.spawnPoint;
         
     }
     
     // Update is called once per frame
     void Update () {
         EnemyPosition();
     }
     
     public void EnemyPosition() {
         enemyPosition.Translate(enemyPosition.forward * Time.deltaTime);
         gameObject.transform.position = enemyPosition.position;
     }
 }

One more thing: If I press the spacebar multiple times in a row the speed of the enemies increase.

Thank you in advance

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 Wiki

Answer by Rahazan · Nov 13, 2013 at 03:08 AM

You are moving the spawnpoint, the enemyPosition pointer points to the Transform of the spawn because of

  void Start () {
   enemyPosition = GameMaster.spawnPoint;
 }

You then move this transform around with the translation.

What you probably want to do is to make the enemy's position the position of the spawnpoint (and then move the enemy around).

You could see the Transform class as a box with some information in it (most importantly position, rotation and scale). With the code above you are making enemyPosition point to that box, and then in EnemyPosition() you changes the values inside that box. That is why your spawnpoint is moving (and with more enemies there are more enemies changing that same transform position, so it moves faster).

What you probably want to do is take information from the spawnpoint box (The Vector3 inside it) and copy it into the box that belongs to the enemy.

So basically said: When you work with a Vector3 (which is a struct) you could see it as a local copy, it works similar as a primitive (int, float, etc.). When you work with a Transform you are working with a pointer to some instance.


Now back to your code

What you do in your Start() method is probably not necessary, as you already instantiate the enemy on the spawnpoint in your GameMaster class.

For the pathfinding, instead, you might want to do is something like this:

  public Transform target;

  void Start () {
   target = GameObject.FindGameObjectWithTag("Evil Player").transform;
 }

And then in your EnemyPosition() function (which you should maybe rename to EnemyPositionUpdate()) you then move your enemy, maybe something like this:

  public void EnemyPosition() {
 
   Vector3 whereIWantToBe = target.position;
   Vector3 direction = whereIWantToBe - transform.position;
 
   transform.Translate(direction * Time.deltaTime);
 
 }

Hope it helps!

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 orjn0101 · Nov 13, 2013 at 06:47 AM 0
Share

Thank you very much for explaining. I think I understand how it works now.

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

17 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

Related Questions

How to make an object go to pre-seted position in world 1 Answer

How to move an object to anothers object position? 1 Answer

Compare Position of two gameobjects and if true, do nothing, help me please! 1 Answer

Different Position (Object position - Mouse Position) 1 Answer

Moving GameObject to various position ? 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