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
1
Question by aopp · Nov 25, 2012 at 10:32 PM · javascriptmovementinstantiationtransform.translate

I need instantiated game objects to move once they're instantiated

My game requires the player to move a Blob around a 2D map and eat the humans that are running around, the problem is the humans don't move. I'm trying to get this chunk of code to work as a base to build from. ## ---- ## (Unityscript) This is supposed to spawn a human and make it move to the left:

 //Speed the humans run at
     var Hspeed : float = 90;
     var human : GameObject;
     function Update() {
 
     //Makes random numbers for the X and Y positions for the spawning humans
     var randX = Random.Range(-725, 590);
     var randY = Random.Range(-400, 375);
     //Makes random number for the probability of a human spawning
     var spawnProb = Random.Range(1, 50);
     
     //Spawns a Human at a random position
     //Moving Left
     if (spawnProb == 3) {
             var person : GameObject = Instantiate(human.gameObject, Vector3 (randX, randY, 1), Quaternion.identity);
             human.transform.Translate(Vector2.right*Hspeed*Time.deltaTime);
         }
   }

----

he humans spawn without any errors or problems but they don't move.

Thanks for the help

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

3 Replies

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

Answer by loopyllama · Nov 25, 2012 at 11:16 PM

That script looks like a spawn manager. In that case you would want a separate script that moves whatever object it is attached to. Example: make a separate script with this:

 var Hspeed : float = 90;
 function Update(){
 transform.Translate(Vector2.right*Hspeed*Time.deltaTime);
 }

and attach it to your human prefab. The reason your humans do not move is because on the frame that the human is created it moves a tiny bit, and it does not update any longer because spawnProb is not equal to 3 the following frame. In order to move all the time, do what I mention above.

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 lacost · Nov 25, 2012 at 11:24 PM

Well taht's is realy weird code, but any way.

1) var person : GameObject = Instantiate ... you just created new humanoid and pu reference on it into person variable, but than you moving your soursed game object human.transform.Translate, not the new one witch created, you should move person not human

2) You do that in update loop, so event if you will do it with the right object, you will do it just in one frame (frame when "spawnProb == 3" and have spawn it), that men's that you will move it just once and even will not see this

Wahi you should do

1)

 var spawnProb = Random.Range(1, 50);
  if (spawnProb == 3) {
      Instantiate(human.gameObject, Vector3 (randX, randY, 1), Quaternion.identity);
     }
  }

modifay spawn section like this, it wierd any way but sould work.

2) Wrine new scrip "HumanoidMotion" - attach it to soursed humanoid that you have `var human : GameObject`, write motin code in Update loop of that attacked script. Good luck bro.

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 aopp · Nov 26, 2012 at 01:11 AM

Thanks, that definitely helped but I still run into a problem. My goal is to have the humans run in a randomly chosen direction (up, down, left, right) and keep going in that direction until they run into something (then they would change direction). Here is what I have for that right now:

 //Decides which direction the humans will run
 function Update() {
 
     var randDir = Random.Range(1,4);
     
     //Right
     if (randDir == 4) {
         person.transform.Translate(-Vector2.right*Hspeed*Time.deltaTime);
     }
     
     //Left
     if (randDir == 3) {
         person.transform.Translate(Vector2.right*Hspeed*Time.deltaTime);
     }
     
     //Up
     if (randDir == 2) {
         person.transform.Translate(Vector2.up*Hspeed*Time.deltaTime);
     }
     
     //Down
     if (randDir == 1) {
         person.transform.Translate(-Vector2.up*Hspeed*Time.deltaTime);
     }
     
 }

This makes the humans basically just shake around. I understand why it does this, but does anybody know what I could do to get going in the right direction?

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 loopyllama · Nov 26, 2012 at 07:56 AM 0
Share

Asking a new question as an answer to your original question is misusing this service. If an answer did answer your question, mark that response as an accepted answer. If you have another question, please start a new question.

avatar image Hunerstrike · Mar 09, 2020 at 02:45 PM 0
Share

This question is 8 years old but im just saying so everyone else with this problem knows

avatar image Hunerstrike · Mar 09, 2020 at 03:28 PM 0
Share

Its because you have the random set to update so it updates the direction every frame fkr every insantiated objective

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

13 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

Related Questions

How to create random movement in 2D 2 Answers

Stick an object to the ground while moving? 1 Answer

Unity is not updating my script/variables 1 Answer

Player launches into air when hitting steep slopes 0 Answers

Make so the script only moves and makes actions to the gameObject that its attached to. 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