It’s a problem with the chapter 8.
I cannot make my avatar turn around while walking.it was ok with the preceeding chapters, no problems, everything just fine. But when it comes to chapter 8, the problem arises. If I press left/right, the avatar just move but cannot turn, I don’t know why?That’s my code until now:
https://www.code3dgames.com/3de/
Thanks for helping.
I’ll need to see your code in order to help you. Unfortunately, you shared a link to the 3DE code editor, not to your code.
To share your code, either use the share option from the menu in 3DE or copy and paste your code directly into your reply. The share option is less error prone. If you prefer copy & paste, be sure to paste it between 3 backtick lines:
Explain how your code is misbehaving...
```
// Paste your code here
```
Include any more information that you like here...
Once you share the code, we ought to be able to figure out the problem relatively quickly.
-Chris
The problem is on line 104, the isWalking()
if-statement:
function turn(){
if(isMovingRight) direction=Math.PI/2;
if(isMovingLeft) direction=-Math.PI/2;
if(isMovingForward) direction=Math.PI;
if(isMovingBack) direction=0;
if(isWalking()) direction=0;
if(direction==lastDirection) return;
lastDirection=direction;
var tween=new TWEEN.Tween(avatar.rotation);
tween.to({y:direction},500);
tween.start();
}
That line should set the direction to 0
if the avatar is not moving. Right now, the code sets the direction to 0
when the avatar is moving. So if the avatar is moving to the right, left, back, or forward, the code faces the avatar forward.
Just change that line to the following to fix it:
if(!isWalking()) direction=0;
Other than missing a single !
, the code looks good!
-Chris
I really appreciate your help. That’s it, I forgot it. And I just want to say the book is very engaging and easy to understand. It is amazing. Thank you for bringing us such a wonderful book. I cannot wait to continue reading the followings. Have a nice day.