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
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;
}
}
Your answer
Follow this Question
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