- Home /
Can't transform position of an object after a certain point. Help?
I'm trying to make a Space Invaders Remake, and I'm having a hard time programming the AI movement. In this case I'm trying to make the enemies move from the left to the right and so on using this code:
using UnityEngine;
using System.Collections;
public class GroupMovement : MonoBehaviour
{
float maxUnits = 2;
float unitsToMove;
Rigidbody2D rb;
bool movingR = false;
bool movingL = false;
void Start()
{
rb = GetComponent<Rigidbody2D>();
InvokeRepeating("moveR", 1, 1);
}
void moveR()
{
movingR = true;
unitsToMove = unitsToMove + 1;
transform.position = new Vector2(rb.position.x + 1, rb.position.y);
}
void moveL()
{
movingL = true;
unitsToMove = unitsToMove + 0.5f;
transform.position = new Vector2(rb.position.x - 1, rb.position.y);
}
void Update()
{
Cancel();
}
public void Cancel()
{
if (unitsToMove > maxUnits && movingR == true)
{
CancelInvoke("moveR");
unitsToMove = 0;
InvokeRepeating("moveL", 1, 1);
}
if (unitsToMove > maxUnits && movingL == true)
{
CancelInvoke("moveL");
unitsToMove = 0;
InvokeRepeating("moveR", 1, 1);
}
}
void moveDown()
{
transform.position = new Vector2(rb.position.x, rb.position.y - 1);
}
}
It works at the begining but once it starts to move left it gets wierd. I don't know if it is some problem with the code it self or just a dumb mistake, but after a couple of moves it starts to transform the position way more than "1".
Could someone help me with this?
Comment