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
1
Question by BramN · May 12, 2016 at 07:57 PM · c#procedural generation

Cube seems to bug a little

I'm currently in the process of remaking Stack and I can currently create a cube, make it move, and let it stop moving on creation of another cube.

However when a cube is created it seems to glitch a little, I think it has something to do with the starting position I set but I'm not sure, and I can't figure it out.

When a cube is created it seems to go to the starting position for a 1000th of a second and then starts moving at another spot and then everything is fine.

Here's the code to create the cube:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class cubeCreator : MonoBehaviour {
 
     float height = 0;
 
     Vector3 positionLeft = new Vector3(0, 0.1f, 0.4f);
     //Vector3 positionRight = new Vector3(1f, 0.1f, 0);
     Vector3 position;
 
     List<GameObject> cubes = new List<GameObject>();
 
     // Use this for initialization
     void Start () {
         
     } 
 
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown("space")) {
             //create cube, little bit higher every time
             GameObject cube = createCube (height);
             //make the cube move
             cube.AddComponent<MoveCube> ();
             //add cube to list
             cubes.Add (cube);
             if (cubes.Count > 1) {
                 //stop moving
                 cubes [cubes.Count - 2].GetComponent<MoveCube> ().enabled = false;
             }
             height += 0.1f;
             //left = -left;
         }
     }
 
     //Creates cube with random color
     GameObject createCube(float y){
         //create values for random color
         float randomR = Random.Range(0f,1f);
         float randomG = Random.Range(0f,1f);
         float randomB = Random.Range(0f,1f);
 
         //create cube
         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         cube.transform.position = positionLeft + new Vector3(0, y, 0);
         //set scale
         cube.transform.localScale = new Vector3(0.6f, 0.1f, 0.6f);
         //set color
         cube.GetComponent<Renderer>().material.color = new Color(randomR,randomG,randomB);
 
         return cube;
 
     }
 }

And the code to make it move:

 using UnityEngine;
 using System.Collections;
 
 public class MoveCube : MonoBehaviour {
 
     private float fromX;
     private float fromY;
     private float fromZ;
 
     private Vector3 from;
     private Vector3 to;
 
     private float secondsForOneLength = 1f;
 
     // Use this for initialization
     void Start () {
         fromY = transform.position.y;
         fromX = transform.position.x;
         fromZ = transform.position.z;
 
 
         from = new Vector3(fromX, fromY, fromZ );
         //to end position; Z-axis = top-left to bottom-right, X-axis = top-right to bottom-left
         to = new Vector3(fromX, fromY, fromZ - 1f );
     }
     
     // Update is called once per frame
     void Update () {
         transform.position = Vector3.Lerp(from, to, Mathf.SmoothStep(0f,1f, Mathf.PingPong(Time.time/secondsForOneLength, 1f)));
     }
 }

Help would be greatly appreciated, and any general remarks on my code is also welcome!

Thanks 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

Answer by BramN · May 12, 2016 at 10:22 PM

Fixed it by following another example in this thread and adding a bool to stop the coroutine instantly without it finishing and then stopping

Here is the code I ended up with (credits to MR_Reptile):

Movecube:

 using UnityEngine;
 using System.Collections;
 
 public class MoveCube2 : MonoBehaviour {
 
     public Vector3 pointB;
     public bool coroutine = true;
 
     IEnumerator Start()
     {
         var pointA = transform.position;
         pointB = new Vector3 (pointA.x, pointA.y, pointA.z - 1f);
         while(true)
         {
             yield return StartCoroutine(MoveObject(transform, pointA, pointB, 1.0f));
             yield return StartCoroutine(MoveObject(transform, pointB, pointA, 1.0f));
         }
     }
 
     IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
     {
         var i= 0.0f;
         var rate= 1.0f/time;
         while(i < 1.0f)
         {
             if (coroutine == false) {
                 yield break;
             }
             i += Time.deltaTime * rate;
             thisTransform.position = Vector3.Lerp(startPos, endPos, i);
             yield return null;
         }
     }
 }

And I modified the update function with

 MoveCube2 cubeStop = cubes [cubes.Count - 2].GetComponent<MoveCube2> ();
                 cubeStop.coroutine = false;

to end up with

 using UnityEngine;
 using System.Collections.Generic;
 
 public class cubeCreator : MonoBehaviour {
 
     float height = 0;
 
     Vector3 positionLeft = new Vector3(0, 0.1f, 0.4f);
     //Vector3 positionRight = new Vector3(1f, 0.1f, 0);
     Vector3 position;
 
     List<GameObject> cubes = new List<GameObject>();
 
     // Use this for initialization
     void Start () {
         
     } 
 
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown("space")) {
             //create cube, little bit higher every time
             GameObject cube = createCube (height);
             //make the cube move - kan verplaatst worden in create cube function
 
             //add cube to list
             cubes.Add (cube);
             if (cubes.Count > 1) {
                 //stop moving
                 MoveCube2 cubeStop = cubes [cubes.Count - 2].GetComponent<MoveCube2> ();
                 cubeStop.coroutine = false;
             }
             height += 0.1f;
             //left = -left;
         }
     }
 
     //Creates cube with random color
     GameObject createCube(float y){
         //create values for random color
         float randomR = Random.Range(0f,1f);
         float randomG = Random.Range(0f,1f);
         float randomB = Random.Range(0f,1f);
 
         //create cube
         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         cube.AddComponent<MoveCube2> ();
         cube.transform.position = positionLeft + new Vector3(0, y, 0);
         //set scale
         cube.transform.localScale = new Vector3(0.6f, 0.1f, 0.6f);
         //set color
         cube.GetComponent<Renderer>().material.color = new Color(randomR,randomG,randomB);
 
         return cube;
 
     }
 }
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Mesh wont render although there are no errors! 1 Answer

Spawning a prefab at a specific point in a procedurally generated map 1 Answer

Difference between the various "OverlapCollider" functions? 0 Answers

Procedurally Generated Terrain 1 Answer

InvalidOperationException: Operation is not valid due to the current state of the object 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