NPC movement

I’m very new to programming and working in unity, and i just started to develop my first game (a 2D side scroller). So the thing i’m trying to do here is to make an enemy NPC walk back and fourth on a platform. The way i first thought of doing this was to make two unrendered boxes with box colliders working as triggers on the edges of the platform so that when the NPC would hit the boxes he would turn around and walk to the other edge and thus hit the other box and do the same thing over again. I don’t know if i did it sort of right but this is the way i did it:

using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour {
	// Public values
	public float Speed;
	// Private values


	// Use this for initialization
	void Start () {
	
	}

	// Object movement
	 
	
		void OnTriggerEnter2D(Collider2D other)
		{
			if(other.name == "EnemyPathBox")
			{
				transform.position = Vector2.right * Speed * Time.deltaTime;
			}
			else if(other.name == "EnemyPathBox1")
			{
				transform.position = Vector2.right * Speed * Time.deltaTime;
			}
		}
	

	// Update is called once per frame
	void Update () {
	
	}
}

Where EnemyParhBox and EnemyParhBox1 are the boxes on the edges of the platform. I think the idea is good enough (i don’t know though) but i just have one problem, whenever the NPC hits one of the boxes it goes to the position [0,0] i have no clue why this is happening. One other thing, I don’t really know how to make it move in the first place so if anyone could help with that too, it would be nice.

Instead of using two colliders on the edges of the platform, you could just parent a single collider to the lower corner of the player with a script on it. Set the collider to trigger, and implement the “OnTriggerExit” function in it. Inside the function, change the character’s direction. This way, you can place the NPC on any platform that drops off and it should correctly stay on the platform (every time the character reaches the edge of the platform, the collider stop colliding with the ground, calls OnTriggerExit, and causes the character to turn around).

If you want to place them on a platform that has walls on either side instead of drops, you could also implement the “OnTriggerEnter” with an if statement checking if the colliding object is a wall, and cause the character to turn around as well.

Also, the reason your NPC is just being teleported to a position is because you’re setting the position to the speed, rather than adding the speed to the position. Every frame you’re setting the position to

Vector2.right * Speed * Time.deltaTime

which is going to be the same number every frame (Vector2.right is a constant value, speed is a constant value, and Time.deltaTime only changes a tiny tiny bit between frames). Since you’re setting it to the same number each frame, it’s not going to move anywhere :stuck_out_tongue: To make it change position, you need to do

transform.position += value;

instead of

transform.position = value;