This has been annoying me for a while and I can't find a solution. Only ground3
moves while the other 2 stay in place. Im using unity and doing a 2d project.
Code:
using System.Collections.Generic;
using UnityEngine;
public class GroundMovement : MonoBehaviour
{
public float globalspeed;
public GameObject Ground1;
public float Ground1Speed;
Rigidbody2D rb1;
public GameObject Ground2;
public float Ground2Speed;
Rigidbody2D rb2;
public GameObject Ground3;
public float Ground3Speed;
Rigidbody2D rb3;
// Start is called before the first frame update
void Start()
{
rb1 = Ground1.GetComponent<Rigidbody2D>();
rb2 = Ground1.GetComponent<Rigidbody2D>();
rb3 = Ground1.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
rb1.velocity = new Vector2(globalspeed + Ground1Speed, 0);
rb2.velocity = new Vector2(globalspeed + Ground2Speed, 0);
rb3.velocity = new Vector2(globalspeed + Ground3Speed, 0);
}
}
The problem is in the
Start()
Method, when you assigned the rigidbodies, you assigned all of them to the rigidbody in theGround1
GameObject, the method should be like this: