Character orientation ThreeJS

Viewed 127

I'm currently working on a small threeJS project. I have a character which have to move on a island. I get all the part with my keys but I'm not able to find how to make the character goes where he look at. The model only rotate on itself and after a lot of researchs I can't find any basic argument or function to do what I want. So is there any argument or function to change the axis orientation or to make my character move where he looks at ?

Thanks all and have some good celebrations

1 Answers

I'm not able to find how to make the character goes where he look at

You can get the direction of your model like so:

const direction = new Vector3();
avatar.getWorldDirection( direction );

You can then translate the avatar along this direction like so:

const displacement = new Vector3();
const offset = 1 * timeDelta; // 1 world unit per second
displacement.copy( direction ).multiplyScalar( offset );
avatar.position.add( displacement );

For a more realistic movement, you should consider to work with a force based steering model. But as a basis, the above code should work.

Related