Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Kmii · Oct 31, 2015 at 03:39 AM · c#scripting probleminvokerepeatingexplode

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 !

Comment
Add comment · Show 2
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 hexagonius · Oct 31, 2015 at 11:05 AM 0
Share

what is the result you want to achieve?

avatar image Kmii hexagonius · Nov 01, 2015 at 10:17 PM 0
Share

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...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Show 8 · 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 Kmii · Nov 01, 2015 at 10:11 PM 0
Share

alt text

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

ffddffd.png (240.7 kB)
avatar image Statement Kmii · Nov 01, 2015 at 11:02 PM 0
Share

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.

avatar image Kmii Statement · Nov 01, 2015 at 11:48 PM 0
Share

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?

Show more comments
avatar image Kmii · Nov 02, 2015 at 03:33 AM 0
Share

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); }

avatar image Statement Kmii · Nov 02, 2015 at 01:20 PM 0
Share
 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); 
 }
avatar image Kmii Kmii · Nov 02, 2015 at 04:37 PM 0
Share

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

avatar image Statement Kmii · Nov 02, 2015 at 04:45 PM 0
Share

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.
   }

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

34 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

Related Questions

The laser problems 0 Answers

Integer arrays not comparing properly. 3 Answers

how do i take whats on one UI canvas and make it apear on another in real time(for a card game) 0 Answers

PlayerScore prefab in unity gives value in gameover screen as 0, how do I show the final value in GameOver scene? 0 Answers

Changing script variable from another script doesn't change it in the original script? 0 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