Ask coding questions

← Back to all posts
Three.js smooth camera
kwe (57)

I don't know how to make it so the transition from changing position more smooth, Any tips or help will be appreciated.

Ignore my horrible formatting and code lmao.

WASD and Q,E to move.

Answered by Coder100 (18922) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18922)

there's a lot of things you can do.

First, use this code instead:

let keys = {};

document.addEventListener("keydown", e => {
  keys[e.which] = true;
});

document.addEventListener("keyup", e => {
  keys[e.which] = false;
});

then, on your animate scene, you add the arrow key movement, like this:

function animate() {
  ...
  
  if (keys[87]) {
    frwd();
  }
  if (keys[83]) {
    bcwd();
  }
  if (keys[65]) {
    cml();
  }
  if (keys[68]) {
    cmr();
  }
  if (keys[69]) {
    right();
  }
  if (keys[81]) {
    left();
  }
}

never ever use else if for movement, it prevents the player from strafing and pressing two keys at once!

kwe (57)

@Coder100 thank you so much! I cant explain how much time you saved me!