[Beginner] trying to make something go back and forth every 0.25 help please :(

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyMovement1 : MonoBehaviour
{

public Rigidbody2D rb;
public float speed = 1f;
[SerializeField] private bool moveLeft = true;
[SerializeField] private bool moveRight = false;
Vector2 movement;

void Start()
{
StartCoroutine(Move());

rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);

}

void Update()
{
movement.x = .25f;

}

void FixedUpdate()
{

if (moveLeft == true)
{
rb.MovePosition(rb.position - movement * speed * Time.fixedDeltaTime);

Debug.Log(“Move left is true”);
}
if (moveRight == true)
{
rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);

Debug.Log(“Move Right is true”);
}

}
IEnumerator Move()
{
if (moveLeft)
{
yield return new WaitForSeconds(0.5f);

moveLeft = false;
moveRight = true;
}
if (moveRight)
{
yield return new WaitForSeconds(0.5f);

moveRight = false;
moveLeft = true;
}

}
}

Why are you writing code for something like that? Why not just use an animation and be done?

Otherwise, here is …

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

Do not TALK about code without posting it. Do NOT retype code. Copy/paste and post code properly. ONLY post the relevant code, and then refer to it in your discussion.

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly