0
Answered

Move Character Prefab to Position using C# script?

brolol.404 3 months ago in Game Creator updated by Marti (Lead Developer) 2 months ago 3

I created a Character GameObject (GameCreator -> Character), created a prefab from that Character GameObject and created a script that instantiates the prefab and then moves it to a position on the scene (defined by an empty GameObject position). The Character prefab spawns successfully and moves to the position; however, the Character slides across the floor without using a walking animation (it uses the correct animation using the GameCreator move Character action, but not in my C# script). How do I script an instantiated  Game Creator Character prefab to move to a new position in C# while using it’s character animations? In addition, the Character prefab has "Can use Navigation Mesh" checked, has a Nav Mesh Agent Component and the NavMesh is baked to the scene; however, the Game Creator Character prefabs do not ignore the NavMesh obstacles and attempt to path directly through them during their slide (while stuck on one of these obstacles, their movement animation plays). How do I make a Game Creator Character prefab move around NavMesh obstacles? Thanks

EDIT: This is the instantiate code line I am using (Unit is the Game Creator Character Prefab):


public GameObject Unit;

Instantiate(Unit, new Vector3(xPosition, YSpawnCoor, zPosition), Quaternion.identity);

and this is the movement line:

public Transform MovementMarker;
transform.position = Vector3.MoveTowards(transform.position, MovementMarker.position, Step);

Unity version:
2019.4.1f1
Game Creator version:
1.1.9
Answered

In order to move around the map, you need to bake the navmesh in your scene. 

As for moving the character, I'm not sure why you're trying to do this using code. There's a handy Action that does it for you called Move Character To. 

Moving a character is a complex chain and it's much better to use the character locomotion system provided by Game Creator, rather than attempting to move the transform itself.

If you still want to use a C# code snippet, take a look at the ActionCharacterMove. In particular, what's inside the Execute() method.

thanks. I was having trouble using the game creator I nstantiate action with character prefabs and then moving those instantiated characters with the game creator move to character action but I will try to figure it out.


is there a game creator action to bake the navmesh during gameplay (after navmesh obstacles move/change)?

Unfortunately no. We use Unity's default navmesh, which must be baked in the Editor. Despite this, there's a Navmesh Components repo on Unity's GitHub that allows to join multiple nav meshes.

To be honest, I haven't tried it yet, but you mind find some useful tools there that might work out for you. Since it integrates with their built-in navmesh, it should work just fine with Game Creator.