How to attach spatial to a camera

I am using CharacterControl for a character, and I want to attach a spatial to the character that will follow the character. I tried updating the spatial position every frame using simpleUpdate(), but the movement is choppy and I can’t figure out a way for the spatial to move along with the camera.

Please help. I checked the java doc but there is no way to attach a spatial to the charactercontrol explicitly.

CharacterControl is a control, meaning it can be attached to spatials in order to directly control them. Create your spatial, then (using Spatial.addControl()) add your CharacterControl to your spatial.

CharacterControl charControl = new CharacterControl();
// customize your CharacterControl
Spatial charSpat = assetManager.loadModel("Models/mod1.j3o");
// customize your Spatial
charSpat.addControl(charControl);
rootNode.attachChild(charSpat);

Look at this tutorial for more on CharacterControl.

Attach your camera to your CharacterControl by setting the location of the camera to the physics location of your CharacterControl. If you want a 3rd person view, look at this tutorial.

In other words, your camera should follow your CharacterControl which is attached to your spatial, instead of having the spatial (and the CharacterControl, I assume) follow the camera.

3 Likes

Okay I understand.

Only problem is my Camera is not rotating, rather the FlyCam. How can I fix this?
Or can I implement this with the flycam rotation ?

The only reason for that problem I can think of, is if you’re calling cam.lookAt() or cam.setRotation().

If that isn’t the case, then I’d have to see your code.

Sounds like OP is using flycam.

…but I think you are trying to help them attach a camera to a spatial when it seems like they want to attach a spatial to a camera.

To be honest, I’m not exactly sure what the end goal is here:
Is the camera locked to the character control or not?
Are you trying to make a spatial that is always visible on screen or something?

Because “attaching a spatial to the character” is really easy. The character control is generally added to some spatial already… then just attach your other spatial to that one.