- Home /
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
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.
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.
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?
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.
This question is 8 years old but im just saying so everyone else with this problem knows
Its because you have the random set to update so it updates the direction every frame fkr every insantiated objective
Your answer
Follow this Question
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