How could I make an NPC?

I have a cat object and i just want it to occasionally move around and rotate. Can anyone teach me how to accomplish this?

There are many ways. I may be able to help you, but your request is pretty broad. Which makes me think that you may be new to programming and PlayCanvas. Instead of showing you exactly how to do it, it would be more beneficial for you if I guide you in the process of figuring out how to do it yourself. So, first, a few questions.

  • Do you have any programming experience or are you new to coding? (either is fine, we all started somewhere)
  • Have you taken a look at the docs?
  • Have you familiarized yourself a bit with the API?
  • Have you taken a look at the tutorials tutorials?
  • Do you have at least a rudimentary understanding of scripting in PlayCanvas or are your starting from scratch?

Answer those questions and I’ll have a better idea of the best way to guide you.

I have some programming experience in python but only the basics in javascript. Ive checked out the tutorials and couldnt find anything that could help me, and i looked at the docs but i dont know were to start.
I do have a basic understanding of scripting in playcanvas.(i think)

Ok. What about 2D or 3D game design? Is this your first project?

I have plenty of game design experience in 2D but not in 3D.

Ok, no worries, it’s similar, we just need to add a Z to your X and Y :wink:

Are you planning to move entities using physics forces or are you just planning to use translation?

Have you played around with moving entities around in PlayCanvas yet? Perhaps a simple player object that you control and move around?

i learned how to make an object move around with the arrow keys. Also I would like to use physic forces to move the object around but i dont know how.

Great, now we are getting somewhere. So, before anything else I would like your to take a look at this very simple project I have made to teach myself PlayCanvas. It uses forces to move a ball around. Also, please take a look at this other tutorial project. Check out the code and try to implement this in your own project to move an object around.

This newfound knowledge may then be enough to get you going with your NPC implementation. Give it a try and write back with any questions if there is something you don’t understand or are struggling with and I’ll try to help.

I get how to make it move now, but how would I make it move around in terms of seconds.
Ex. Every two seconds rotate the object and move forward a little bit
i know how to rotate and move but how would i make it do that every couple of seconds?

Excellent question. I’d like you to try to figure this out. If you have a bit of game programming knowledge, I’m pretty sure you can. Have you tried something yet? What do your programmer instincts tell you? How do you think we can we make something, do something, every X seconds? Hint: take a good look at the dt parameter being passed to the update function.

This is what i put

pc.script.create('cat', function (app) {
    // Creates a new Cat instance
    var Cat = function (entity) {
        this.entity = entity;
    };

    Cat.prototype = {
        // Called once after all resources are loaded and before the first update
        initialize: function () {
        },

        // Called every frame, dt is time in seconds since last update
        update: function (dt) {
            if (this.local) {
        this.entity.rotateLocal(0.1, dt, 0);
    } else {
        this.entity.rotate(0, this.speed * dt, 0);
    }

        }
    };

    return Cat;
});

When i load up the game i dont see the cat anywhere.

Did you append this script to an entity to make it run?

@scottthepotato Looks like you are using the legacy scripting system from the code you have shown? Is this deliberate?

The closest tutorials that will help with ‘prodding’ an object every X seconds is:

https://developer.playcanvas.com/en/tutorials/Using-forces-on-rigid-bodies/

And this one for ‘timed’ events:
https://developer.playcanvas.com/en/tutorials/changing-textures-at-runtime/

yes i put it onto the cat object

Try writing your code in this style instead:

var Cat = pc.createScript('cat')

// Called once after all resources are loaded and before the first update
Cat.prototype.initialize = function() {

}

// Called every frame, dt is time in seconds since last update
Cat.prototype.update = function() {

}