A way to Rotate with Script

28 views
Skip to first unread message

Jowito 2000

unread,
Oct 4, 2017, 1:31:13 PM10/4/17
to CoSpaces

Hi again. I'm still coding my world in script but when i wanted to rotate the object, there is no way to rotate it. ill explain. there is a code to rotate the object through a vector; addLocalRotation, but when it starts to rotate i don't know to stop the rotating when i want. and there is also another code for another rotation; addRotation, that rotates the thing but dont rotate the object around itself, thats what i want. i don't know if i explained well. but if anyone know something or could help i would be very thankful

Benjamin Singh

unread,
Oct 5, 2017, 4:29:37 AM10/5/17
to CoSpaces
Hello,

in the documentation for the .addLocalRotation() method we use Scene.scheduleRepeating() to add a rotation for each frame. If you want to stop the rotation you can add a check and stop the update loop (Scene.scheduleRepeating()) calling the .dispose() method if the check doesn't pass. Here is a little commented example on how you may do it:

var man = Scene.createItem('LP_Man', 0, 0, 0);

var rad = 0;                    // radians total
var dr = 0.01;                  // radians delta
var limit = Math.PI * 0.5;      // radians limit

var update = Scene.scheduleRepeating(function() {
   
if(rad >= limit) update.dispose() // if total radians exceeds limit => stop update loop

   
// for each frame add dr to total radians and rotate object by dr around local vector
    rad
+= dr;
    man
.addLocalRotation(0, 0, 0, 0, 0, 1, dr);
}, 0);

Here we're rotating an object around the local vector (0, 0, 1) for PI/2 radians (90°), then we stop it. Here is the example in action: https://cospac.es/P2BQ

In general, you should use .addLocalRotation() if you want to rotate an object around itself. Also, in CoSpaces all angle measurements are in radians, not in degrees. I hope this helps.

Benjamin

Jowito 2000

unread,
Oct 5, 2017, 4:45:32 AM10/5/17
to CoSpaces
Wow. Really thank you. It's just perfect with it. I'll introduce it in my world. I'm very thankful for your help :D. you just solved my world jajajajaj

Benjamin Singh

unread,
Oct 5, 2017, 10:24:01 AM10/5/17
to CoSpaces
You're welcome! Glad that it worked.

Benjamin
Reply all
Reply to author
Forward
0 new messages