Hi , I am very new to Babylon and Unity . I want to rotate my character towards a given point where he is supposed to reach while running . I am able to rotate the character towards the direction of movement in unity . But not via BabylonJS.
here is the piece of code that I wrote -
public void moveTowards(Vector3 a , Vector3 b )
{
if(b-a!=Vector3.zero){
Quaternion toRotation = Quaternion.LookRotation(b-a,Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation,toRotation,rotationSpeed*Time.deltaTime);
}
}
Please help me write the equivalent BabylonJS function or suggest me some other way to do it ? I tried several answers but none helped .
Wants to provide an additional example of the animation for reference. Fun fact you can stack animations too. For example, you do rotation and movement at the same time. Wrote up this quick example where I just reused the same keyframes for both of my desired movements.
const xRotate = new BABYLON.Animation("xRotate", "rotation.y", frameRate, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
const xSlide = new BABYLON.Animation("xSlide", "position.x", frameRate, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
const keyFrames = [];
keyFrames.push({
frame: startFrame,
value: 2
});
keyFrames.push({
frame: endFrame,
value: -2
});
xSlide.setKeys(keyFrames);
xRotate.setKeys(keyFrames);
box.animations.push(xRotate);
box.animations.push(xSlide); //push a second animation that will happen at the same time.
scene.beginAnimation(box, startFrame, endFrame, false);
@msDestiny14@sebavan Thanks for answering . I went through the animation docs and was able to gain a lot of information .I know how to stack up the animations and I have the target coordinate for player and its current coordinate but I am unable to rotate him in the desired direction . I tried calculating the angle between the vectors and rotating the mesh by that angle but still it is not working as desired . Can you help me re-write the above unity code in babylon.JS ?
I tried using the “.lookAt” method
let direction = targetPosition.subtract(PlayerModel.position)
playerModel.lookAt(direction);
I couldn’t find any utility method like lookRotation or rotateTowards which did the same job in unity . If you know the similar alternative for these methods in babylon.js , then please do tell me . Thanks !
Now here’s a deep question I’m wondering. Every once in a while we get question like this where having a helpful function could be useful, especially if users are coming from other tools that have these built functions.
Should I go ahead and implement this type of function. For example: Mesh.lookAt(targetPosition: vector3) ? We have a couple of frequently repeated question/answers so it begs the question would this be helpful for bloating the engine.
Or option 2: Adding these examples to the documentation so its easier for people to find without having to dig.