box won't move to the left to right it should, but for me it only stand still. Here's my code if you see an error on it. thanks! Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class BoxScript : MonoBehaviour { private float min_X = -2.2f, max_X = 2.2f; private bool canMove; private float move_Speed = 2f; private Rigidbody2D myBody; private bool gameOver; private bool ignoreCollision; private bool ignoreTrigger; void Awake() { myBody = GetComponent<Rigidbody2D>(); myBody.gravityScale = 0f; } // Start is called before the first frame update void Start() { canMove = true; if(Random.Range(0, 2) > 0) { move_Speed *= -1f; } // Update is called once per frame void Update() { MoveBox(); } void MoveBox() { if(canMove) { Vector3 temp = transform.position; temp.x += move_Speed * Time.deltaTime; if(temp.x > max_X) { move_Speed *= -1f; } else if (temp.x < min_X) { move_Speed *= -1f; } transform.position = temp; } } } }
What do you think how other people solve such problems? It's called "debugging". This means that you try to find the source of the error. The easiest thing is to place Debug.Log messages at strategical positions in your code and verify the validity of your assumptions. What values does the variables have? Which path does the program flow take? What are the results of the calculations? You simply can't ask the forums for every tiny error/bug/problem you have. If you want to be a programmer you HAVE TO learn how to debug. Start right away and just do it. This way you will learn much more about programming and how computers work than by copying some corrected code someone posted.