Problems with scripting and invokeRepeating
I want to start explaining that my english is not so good, so ill try me best. This is my problem:
im making a "Hole in wall" game and im trying to Spawn 3 walls to test my script, i used invokerepeating for this task and invoking coroutines to create my wall and move it . i dont know if im making myself clear, but i hope u guys can undertand the idea. so... the problem is that after the first repeat my wall went to the original point and explode ( my wall was made by cubes order by a matrix), so when de second one comes,it explotes too. how can i fix this :/? this is my gameManager code. (C#)
using UnityEngine;
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using System;
public class GameManager : MonoBehaviour {
public WallMaking Wall;
float timer = 0f;
// Use this for initialization
public void Spawner()
{
timer += Time.deltaTime;
//InvokeRepeating("Spawner()", 60f, 3f);
Debug.Log("SPAWNEA CTM!");
Debug.Log(timer);
StartCoroutine(Wall.WallMaker());
Vector3 Source = new Vector3(661.49f, 0.2f, 3.52f);
Vector3 Target = new Vector3(661.49f, 0.2f, -4.87f);
StartCoroutine(Wall.WallsMovement(Source, Target, 5f));
Debug.Log("hola");
Debug.Log(timer);
}
void Start() {
Debug.Log("A VER");
InvokeRepeating("Spawner", 1f, 5f);
Debug.Log("SE REPITE");
}
//float timer = 0f;
}
and this is my wallmaking code:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
// C#
public class WallMaking : MonoBehaviour
{
int[,] mat ={
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1},
};
int[,] mat2 ={
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,1,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1},
};
int[,] mat3 ={
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1},
};
int[,] matO;
List<int[,]> wallList = new List<int[,]>();
public IEnumerator WallMaker()
{
wallList.Add(mat);
wallList.Add(mat2);
wallList.Add(mat3);
System.Random rnd = new System.Random();
matO = wallList[rnd.Next(wallList.Count)];
float cubesizeX = 0;
float cubesizeY = 0;
for (int y = 0; y < 11; y++)
{
for (int x = 0; x < 11; x++)
{
if (matO[y, x] == 1)
{
GameObject Brick = GameObject.CreatePrimitive(PrimitiveType.Cube);
Brick.transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, transform.localScale.z);
Brick.AddComponent<Rigidbody>().useGravity = false;
Vector3 newPos = new Vector3((transform.position.x) + cubesizeX, (transform.position.y) + cubesizeY, transform.position.z);
Brick.transform.position = newPos;
Brick.transform.parent = GameObject.Find("MainWall").transform;
}
cubesizeX = cubesizeX + 0.3F;
}
cubesizeX = 0;
cubesizeY = cubesizeY + 0.3F;
}
Debug.Log("MAKING A WALL");
yield return null;
}
public IEnumerator WallsMovement(Vector3 source, Vector3 target, float overTime)
{
float startTime = Time.time;
while (Time.time < startTime + overTime)
{
Debug.Log("TIEMPO DE MOVERTE");
Debug.Log(startTime + overTime);
transform.position = Vector3.Lerp(source, target, (Time.time - startTime) / overTime);
yield return null;
}
transform.position = target;
}
//void Update() {
//KinectManager manager = KinectManager.Instance;
//uint playerID = manager != null ? manager.GetPlayer1ID() : 0;
//if (playerID > 0) {
// transform.Translate (-Vector3.forward* 2.0f * Time.deltaTime, Space.Self);
//}
// Debug.Log("MUEVETE");
//yield return null;
// }
}
hope u guys can helpme !
ohh sorry dindt see this before :C but i just coment below what i wanted to do :O , create a Wall, move it, create a Wall, move it... repeat repeat...
Answer by Statement · Oct 31, 2015 at 03:32 PM
the problem is that after the first repeat my wall went to the original point and explode
Well, you call WallsMovement() on the same object repeatedly. WallsMovement sets transform.position to lerp from source to target, which are constant positions in the world. It sounds like you want to have multiple wallmakers and not just one so each wall maker has its own motion?
yes somethign like that, i want this thing to créate a Wall then move that Wall from its original point to another point, and then when the Wall reaches the other point , créate another Wall (in the original point) and move it... and repeat repeat repeat N times. :/ now my Unity doesnt respond when wallsmovement is called .__. help -.- , i even thought using destroygameobject after the Wall moves , but no... doesnt Works xD
pd: this is how my game looks right now :C
Try unparenting the cubes from the wall when you reach the end. By the way, you probably shouldn't parent the cubes to begin with. Physics get weird when rigidbodies are parented to moving objects.
You could give the boxes some force with Rigidbody.AddForce at the start. That way you don't even need to move the wallmaker. But if they hit something, it'll be very obvious that you have no gravity enabled. But it could be a push toward a better direction at least.
my "parent" its call $$anonymous$$ainWall and when i make the wall all the cubes became childs of $$anonymous$$ainWall , i did this so when i move the wall all the cubes move at the same time, if i change that how can i make all the cubes move at the same time :S?
Statement
i almost did it!, but now i have a new problem -.- i used the code below on my Wall$$anonymous$$aking class and it almost work, my problem now is that the "float z" goes from like 3 to -3 and then goes back to -3 to 3 .__. why is that -.-?
void FixedUpdate() { rb.AddForce(0,0,((-(GameObject.Find("$$anonymous$$ainWall").transform.position) - transform.position) * 500 * Time.smoothDeltaTime).z); }
void FixedUpdate()
{
rb.AddForce(0,0,((-(GameObject.Find("$$anonymous$$ainWall").transform.position) - transform.position) * 500 * Time.smoothDeltaTime).z);
}
Let's make that readable...
void FixedUpdate()
{
GameObject wall = GameObject.Find("$$anonymous$$ainWall");
Vector3 offset = -wall.transform.position - transform.position;
float z = offset.z * 500 * Time.smoothDeltaTime;
rb.AddForce(0,0, z);
}
So, for z to become negative, it means that mainWall passes through the XY plane of this object.
Try and think of it like this. Right is Z, Down is Time.
wall this
wall this
wall this
this wall
this wall
this wall
Over time, the wall moves from left to right side of "this" (or relatively speaking, "this" moves from right so left side of wall)
Consider keeping a constant direction.
Vector3 constantForce;
void Start()
{
GameObject wall = GameObject.Find("$$anonymous$$ainWall");
if (!wall) {
print("Could not find $$anonymous$$ainWall!");
return;
}
Vector3 offset = wall.transform.position - transform.position;
constantForce = Vector3.Scale(offset, Vector3.forward * -500);
}
void FixedUpdate()
{
rb.AddForce(constantForce * Time.deltaTime);
}
as you said, i tried this, but it doesnt work -.- walls are not moving at all :/
Vector3 constantForce;
void Start()
{
GameObject wall = GameObject.Find("$$anonymous$$ainWall");
if (!wall) {
print("Could not find $$anonymous$$ainWall!");
return;
}
Vector3 offset = wall.transform.position - transform.position;
constantForce = Vector3.Scale(offset, Vector3.forward * -500);
}
void FixedUpdate()
{
rb.AddForce(constantForce * Time.deltaTime);
}
Then i tried this, and well as you know, is practically the same thing i had, so its still moving -4 to 4 and then 4 to -4, my walls are dancing xD, its funny and sad at the same time -.-
void FixedUpdate() { GameObject wall = GameObject.Find("$$anonymous$$ainWall"); Vector3 offset = -wall.transform.position - transform.position; float z = offset.z 500 Time.smoothDeltaTime; rb.AddForce(0,0, z); }
T___T
It's probably because it's positioned at the same z as mainwall. Just give it any direction.
void Start()
{
constantForce = Vector3.forward * 500; // Note. This could be ridiculously high.
}