Making an NPC go up and down in UNITY

unity npc script
make an npc move unity
unity 2d npc movement
unity npc random movement
making npc in unity
unity npc character controller
unity npc class
unity 2d enemy patrol

So I have this code, it is to make an enemy up and then down using waypoints, it actually works, but for a strange reason after some looping between the waypoints it goes all the way up to the infinity passing the top waypoint (wp0), why is this happening??? I've been toying whith the code but nothing get the appropiate behaviour.

The code is for an NPC which has a kinematic rigidbody, it "floats" in the air and is supossed to fly up and down like the flying koopas in the mario bros game...

public GameObject[] myWaypoints;
float direction = 1f; //1 up, 0 stop. -1 down.
int _myWaypointIndex = 0; // used as index for My_Waypoints
float _moveTime;
float _vx = 0f;
bool _moving = true;
public float waitAtWaypointTime = 1f;   // how long to wait at a waypoint
public bool loopWaypoints = true; // should it loop through the waypoints

void EnemyMovement()
    // if there isn't anything in My_Waypoints
    if ((myWaypoints.Length != 0) && (_moving))

        // make sure the enemy is facing the waypoint (based on previous movement)

        // determine distance between waypoint and enemy
        _vx = myWaypoints[_myWaypointIndex].transform.position.y - _transform.position.y;

        direction = Mathf.Sign(myWaypoints[_myWaypointIndex].transform.position.y);
        Debug.Log("Direction = " + direction);

        // if the enemy is close enough to waypoint, make it's new target the next waypoint
        if (Mathf.Abs(_vx) <= 0.05f)
            Debug.Log("changin to the next waypoint");

            // At waypoint so stop moving
            _rigidbody.velocity = new Vector2(0, 0);

            // increment to next index in array

            Debug.Log("_myWaypointIndex = " + _myWaypointIndex);
            Debug.Log("Length = " + myWaypoints.Length);
            direction *= -1;
            Debug.Log("New Direction = " + direction);

            // reset waypoint back to 0 for looping
            if (_myWaypointIndex >= myWaypoints.Length)
                if (loopWaypoints)
                    _myWaypointIndex = 0;
                    _moving = false;

            // setup wait time at current waypoint
            _moveTime = Time.time + waitAtWaypointTime;
            // enemy is moving
            _animator.SetBool("Moving", true);

            // Set the enemy's velocity to moveSpeed in the y direction.
            _rigidbody.velocity = new Vector2(0.0f, _transform.localScale.y * moveSpeed * direction);



I think that the problem is when you reset the _myWaypointIndex to 0, loopWaypoints is in some other function, but the else doesn't make the NPC to stop, so maybe keeps moving to the next waypoint that it can not find beacause the index haven't been reset so the _vx distance will never be 0, try to check if the loopWaypoints change the value when you want, the error maybe there.

        if (loopWaypoints)
            _myWaypointIndex = 0;
            _moving = false;
            //Stop the movment 
            moveSpeed = 0;

Making an NPC go up and down in UNITY, So I have this code, it is to make an enemy up and then down using waypoints, it actually works, but for a strange reason after some looping� Ever wanted to make an object float up and down while spinning in Unity? Well, I’ve written a script for that. Usage. Select an object in your Unity project. Add Component > New Script Name: Floater; Language: C-Sharp; Edit the script; Copy & Paste the code below into your script editor. Save the script. Tweak settings to your heart’s content

Maybe: what happens if the enemy passes the last waypoint and _vx> 0.05f?

it wouldn't be catched in:

if (Mathf.Abs(_vx) <= 0.05f)

Making NPC's wander in 2D, Come check them out and ask our experts any questions! The possible directions that the object can move int, right, left, up, down, and zero I'm trying to use this script to make an NPC "wander" in my 2D top down game. This array can be assigned from the inspector A Unity window that displays information about the currently selected GameObject, asset or project settings, alowing you to inspect and edit the values. More info See in Glossary using GameObjects The fundamental object in Unity scenes, which can represent characters, props, scenery, cameras

The problem is that in Mathf.abs(_vx) <= 0.05f _vx value is never reaching below 0.05f.

What u should do is attach a trigger at waypoint and change the waypoint coordinate when player come in contact with that trigger.

Or do this.

_vx =   Mathf.Abs(myWaypoints 
transform.position.y) - 

//And then write.

if (_vx <= 0.5f)
   // change coordinate.'''

[ENG] How to make a friendly NPC move automatically in Unity , float for animation state, changed by code: gets the speed on the y-axis (up or down Duration: 9:32 Posted: Feb 25, 2016 Instead, to control the height of the up and down movement, you should be multiplying the variable by the newY. Also the newY will will be going around the 0 coordinate. To fix this you should add the old position to the newY float newY = Mathf.Sin(Time.time * speed) * height + pos.y;

Unity RPG Tutorial #26 - NPC Movement, Learn how to make your own RPG game using Unity! In this episode we're going to add an Duration: 20:28 Posted: Apr 1, 2016 Unlike Vector3.up, Transform.up moves the GameObject while also considering its rotation. When a GameObject is rotated, the green arrow representing the Y axis of the GameObject also changes direction. Transform.up moves the GameObject in the green arrow’s axis (Y). For moving the GameObject on the Y axis while ignoring rotation, see Vector3.up.

Mini Unity Tutorial, JIMMYVEGAS In this Mini Unity Tutorial we show you how to create a script which will make an Duration: 10:42 Posted: Feb 2, 2018 Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.

How to make an NPC move in Unity, My suggestion is learn how to make things move and give GameObject's 'tasks' to types, and possible ways to make an NPC move with different complexities and //Go up. transform.position += new Vector3 (0,Speed*time.deltaTime,0);. }else{ you might want to check out other ones first at In this Unity tutorial, I will show you how to code a simple patrol script in C# that can then be used in what ever 2D or 3D game you are currently developping ! Basically we will get a character