public Transform leftPoint;
public Transform rightPoint;
public float moveSpeed;
private Rigidbody2D myRigidbody;
public bool movingRight;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent();
}
// Update is called once per frame
void Update()
{
if (movingRight && transform.position.x > rightPoint.position.x)
{
movingRight = false;
}
if (!movingRight && transform.position.x < leftPoint.position.x)
{
movingRight = true;
}
if (movingRight)
{
myRigidbody.velocity = new Vector3(moveSpeed, myRigidbody.velocity.y, 0f);
}
else
{
myRigidbody.velocity = new Vector3(-moveSpeed, myRigidbody.velocity.y, 0f);
}
}
}
sorry i got it working
{
public Transform leftPoint;
public Transform rightPoint;
public float moveSpeed;
private Rigidbody2D myRigidbody;
public bool movingRight;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent();
}
// Update is called once per frame
void Update()
{
if (movingRight && transform.position.x > rightPoint.position.x)
{
transform.localScale = new Vector3(1f, 1f, 1f);
movingRight = false;
}
if (!movingRight && transform.position.x < leftPoint.position.x)
{
movingRight = true;
}
if (movingRight)
{
transform.localScale = new Vector3(-1f, 1f, 1f);
myRigidbody.velocity = new Vector3(moveSpeed, myRigidbody.velocity.y, 0f);
}
else
{
myRigidbody.velocity = new Vector3(-moveSpeed, myRigidbody.velocity.y, 0f);
}
}
}