# Mesh Go, Stop, Rotate, Go!

**URL:** <https://forum.babylonjs.com/t/mesh-go-stop-rotate-go/10786>\
**Category:** Questions\
**Created:** [May 12, 2020, 8:55pm UTC](https://forum.babylonjs.com/t/mesh-go-stop-rotate-go/10786 "2020-05-12T20:55:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anderson34](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@Anderson34](https://forum.babylonjs.com/u/Anderson34)\
**Post date:** [May 12, 2020, 8:55pm UTC](https://forum.babylonjs.com/t/mesh-go-stop-rotate-go/10786/1 "2020-05-12T20:55:00Z")

</div>

Hello everyone,

I’m working on game character AI for movement. Here is what the mesh is supposed to do:

1. Move for a certain amount of time along X/Z axes (let’s say 3 seconds).
2. After 3 seconds, stop, rotate along the Y axis for a random value (Math.PI, Math.PI/2, Math.PI/4, etc.).
3. After rotation, travel along **LOCAL** X/Z axes for 3 seconds.
4. Repeat on loop.

Here’s the PG I created that incorporates some of these elements:  
[https://www.babylonjs-playground.com/#GRXDS7](https://www.babylonjs-playground.com/#GRXDS7)

I’m obviously missing the timed stops, and the movement along updated local axes 🤔

---

<div class="post-metadata">

**Author:** ![Raggar](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raggar/32/20672_2.png) [@Raggar](https://forum.babylonjs.com/u/Raggar)\
**Post date:** [May 12, 2020, 9:34pm UTC](https://forum.babylonjs.com/t/mesh-go-stop-rotate-go/10786/2 "2020-05-12T21:34:22Z")

</div>

If you want the local forward direction, you can use the matrix of the sphere mesh:  
[https://www.babylonjs-playground.com/#GRXDS7#1](https://www.babylonjs-playground.com/#GRXDS7#1)

---

<div class="post-metadata">

**Author:** ![Anderson34](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@Anderson34](https://forum.babylonjs.com/u/Anderson34)\
**Post date:** [May 12, 2020, 9:46pm UTC](https://forum.babylonjs.com/t/mesh-go-stop-rotate-go/10786/3 "2020-05-12T21:46:55Z")

</div>

This is exactly right, thank you @Raggar! 😃 👍

I’ll need some time to study and fully understand this code, but the mesh is moving correctly.

There’s one other element that I can’t seem to figure out. Any suggestions for how to get sphere2 (the grey one) to follow the moving sphere around the map?

Example:  
Based on the grey sphere’s speed, it will be able to follow and eventually collide with the textured sphere, or it will just continue to try to move to the textured sphere’s current XYZ position if its speed is too low.

Thanks again for the help!

---

<div class="post-metadata">

**Author:** ![Raggar](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raggar/32/20672_2.png) [@Raggar](https://forum.babylonjs.com/u/Raggar)\
**Post date:** [May 12, 2020, 10:06pm UTC](https://forum.babylonjs.com/t/mesh-go-stop-rotate-go/10786/4 "2020-05-12T22:06:56Z")

</div>

That depends on how you want it to behave. There are multiply ways of achieving that.  
You can use the direction between the two meshes, you can interpolate between the positions, you can rotate the grey sphere toward the other one and use the local forward axis like above. It’s a matter of what’s needed in the end. This is probably the easiest way: [https://www.babylonjs-playground.com/#GRXDS7#2](https://www.babylonjs-playground.com/#GRXDS7#2)

---

<div class="post-metadata">

**Author:** ![Anderson34](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@Anderson34](https://forum.babylonjs.com/u/Anderson34)\
**Post date:** [May 12, 2020, 11:08pm UTC](https://forum.babylonjs.com/t/mesh-go-stop-rotate-go/10786/5 "2020-05-12T23:08:12Z")

</div>

Once again, you accomplished in a few minutes what I’ve been stuck on for days 😂 👍

Being new to programming, I have about a dozen other questions about _ **why** _ this code works, but I’ll study it and try to figure it out over time.

Thanks again!
