#make an entity rotate slowly

1 messages · Page 1 of 1 (latest)

tribal tinselBOT
#
Thanks for asking your question!

Once you have finished, please close your thread.

lavish light
#

you can use a tick event and delta value by which u rotate entity

lastrot = null
delta = 0.5
onTick():
  if not lastrot:
    lastrot = player.rot
    return

  player.rot = rot + delta
sturdy cove
#

i have only an problem on that, i have never used an tick event or delta value before.

#

could you maybe show me how to use those?

lavish light
#

if you explain what exactly u want, then

sturdy cove
#

My plan is that when the entity ( that is floating and moving ) when it turns, it does not do it suddenly, but little by little, that is, more slowly.

lavish light
#
function fromAngle (pitch, yaw, distance = 1) {
    let pitchRad = this.degreeToRadians(pitch)
    let yawRad = this.degreeToRadians(yaw)
    
    let sinYaw = Math.sin(yawRad)
    let sinPitch = Math.sin(pitchRad)
    let cosYaw = Math.cos(yawRad)
    let cosPitch = Math.cos(pitchRad)
    
    return {
        x : distance * -sinYaw * cosPitch ,
        y : distance * -sinPitch ,
        z : distance * cosYaw * cosPitch
    };
}
    
function toAngle (x, y, z) {
    let magnitude = Math.sqrt(x*x + y*y + z*z)
    
    x /= magnitude
    y /= magnitude
    z /= magnitude
    
    let pitch = this.radianToDegrees(Math.asin(-y))
    let yaw = this.radianToDegrees(Math.atan2(-x, z))
    
    return { pitch, yaw }
}

function rotateEntitySlow (entity, targetRotation, duration = 1) {
    let delta = 1 / duration
    let v = fromAngle( targetRotation.x, targetRotation.y )
    
    let t = 0
    
    return new Promise( resolve => {
        
        system.run( function runnable() {
            if (t <= 1) {
                system.run( runnable )
            } else {
                resolve()
            }
            
            let { x, y, z } = fromAngle( entity.rotation.x, entity.rotation.y )
            
            x += (v.x - x) * t
            y += (v.y - y) * t
            z += (v.z - z) * t
            
            let { pitch, yaw } = toAngle( x, y, z )
            
            entity.rotation.x = pitch
            entity.rotation.y = yaw
            
            t += delta;
            
            
        } )
        
        
    } )
}
#
let targetRotation = { x: 0, y: 0 }

async function rotate() {
  await rotateEntitySlow( player, targetRotation, 10 )

  console.warn( 'rotation done' )
}

rotate()
#

@sturdy cove

sturdy cove
#

oh, thanks dude

#

so i put this in the entity file right?

lavish light
#

ye

sturdy cove
#

thanks dude

#

let me try it

lavish light
#

test it for errors

#

cuz i dont test ingame

#

rotateEntitySlow() is the main function it takes enity, target rotation and duration in ticks

if u want to run something after rotating is done use a async function with await
like shown in example

sturdy cove
#

alr

#

where should i put this? [inside of the entity file]

lavish light
#

on top

sturdy cove
#

alr thanks

lavish light
#

do it now

#

to fix bugs

sturdy cove
#

I'm so sorry if I don't know much about it, I've never done anything like this before

lavish light
#

thats not script api

#

thats other thing

#

thats more of #old-animations thing scripting is a #old-script-api thing

sturdy cove
#

oh

lavish light
#

well u was not clear what ur doing but i think u are just trying for #old-animations not #old-script-api