- Home /
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.
Thanks for the help. Well it definitely did something different. Now it's stuck in [0,0].
Don't forget to set the speed to a value greater (or lower) than 0.
$$anonymous$$oreover, your enemy is not in the center of the scene I guess and you have placed it in a specific place. Then, gets its initial position in the Start function, and in the Update :
transform.position = Vector2.right * Speed * Time.deltaTime + initialPosition;
(initialPosition is a private Vector3 of your class)
In the Start function :
initialPosition = transform.position ;
Oops, I am not familiar with 2D games, sorry.
Are you sure the Speed value is different from 0 ?
Are you sure the enemy object holds the script ?
Are you sure the script hold by the enemy is enabled ?
Do your colliders are touching each other ?
If so, they should not. They must be set as follow :
[]__________[]
The [] are colliders The __ are your platform
That's strange. $$anonymous$$ake sure the Update is running : add a Debug.Log("Update is running");
There is another solution I guess, but I think the one we are trying to implement is the easiest.
Answer by alexi123454 · Jul 22, 2015 at 02:29 PM
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 :P To make it change position, you need to do
transform.position += value;
instead of
transform.position = value;
Ahhhh, totally forgot OnTriggerExit even exists. Thank you for the help.