Hello there. I am currently working on a project for my beginning programming class and have a problem. My enemy is supposed to go left and right on the screen and go back and forth while dropping objects toward the player. In my code the enemy goes left and then stops at the edge of the screen but wont go in the other direction. Any help would be appreciated. I am using C#. Thank you for your time.
Code:
using UnityEngine;
using System.Collections;
public class ThiefMove : MonoBehaviour
{
public int moveSpeed = 140; //per second
Vector3 computerDirection = Vector3.left;
Vector3 moveDirection = Vector3.zero;
Vector3 newPosition = Vector3.zero;
void Start ()
{
}
void Update ()
{
Vector3 newPosition = new Vector3(-1,0,0) * (moveSpeed * Time.deltaTime);
newPosition = transform.position + newPosition;
newPosition.x = Mathf.Clamp(newPosition.x, -101, 126);
transform.position = newPosition;
if(newPosition.x > 126)
{
newPosition.x = 126;
computerDirection.x *= -1;
}
else if(newPosition.x < -101)
{
newPosition.x = -101;
computerDirection.x *= -1;
}
}
}