So, I’m basically trying to move an object from the right side to the left side. I’m trying this code but every time I run the game, after a certain point it starts to somehow to subtract way more than what the code says. Even before reaching the negative axis. Could someone help me please?
using UnityEngine;
using System.Collections;
public class GroupMovement : MonoBehaviour
{
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
InvokeRepeating("moveR", 1, 1);
StartCoroutine(Wait());
}
public void moveR()
{
transform.position = new Vector2(rb.position.x + 1, rb.position.y);
}
public void moveL()
{
transform.position = new Vector2(rb.position.x - 1, rb.position.y);
}
void Update() {
}
public void CancelR()
{
CancelInvoke("moveR");
Wait();
}
public void CancelL()
{
CancelInvoke("moveL");
Wait();
}
void moveDown()
{
transform.position = new Vector2(rb.position.x, rb.position.y - 1);
}
IEnumerator Wait()
{
yield return new WaitForSeconds(1);
Debug.Log("Wait");
}
}