Using tween library

Hello guys im trying to use tween library, i have put tween script in my project and tried to use smoothLookAt but in return i have received just error log output.
does anybody know where issue might be ?


image

Hi @smokys,

is this.lookTo undefined?

It is defined in the code

What is the result of console.log(this.lookTo);?

‘undefined’

Then it’s not defined correctly. Are you sure it’s defined in the same script?

it says that there is something wrong in this line this.lookTo.copy(this.targets[this.targetIndex].getPosition());

targets is defined correctly

It’s not the solution, but try to set this.lookTo = new pc.Vec3; just before that line to see if that solve the problem.

it throws other error

Thats a funny one. Did you get that error at the line that I suggested to add?

No, still the same line

I think that’s something with the position of the target that you try to copy. First replace the line that I suggested to the initialize function. Then the first problem in this line is solved.

you mean pc.Vec3(); ?

i forgot to mention that this is the line in initialize function

image

edit: i changed to
image

and new output is

the same error log output throws when i use new pc.Vec3();

Replace this.lookTo = new pc.Vec3; to the initialize function.

Cannot read property 'copy' of undefined

Can you show the script here place?


//Script pre pohyb hraca a nastavanie animacii (necinny,beh,finish_dance, pad)

var PlayerMovement = pc.createScript('plyerMovement');

//defaultna a maximalna rychlost hraca,animacia padajuceho hraca, pocet waypointov na mape (nastavitelne v inspectori)
PlayerMovement.attributes.add('playerSpeed', {
    type: 'number',
    default: 10
    
});
PlayerMovement.attributes.add('playerMaxVelocitySpeed', {
    type: 'number',
    default: 4
    
});

PlayerMovement.attributes.add("fallAnimation", {
   type: 'boolean',
    default: true
    
});

PlayerMovement.attributes.add("maxWayPoints", {
   type: 'number',
    default: 20
    
});

var playerSpeed = 0;

//take notice i have playerSpeed in attributes add
//nacitanie stavu animacii

PlayerMovement.states = {
    
    idle : {
        animation: 'standing.glb'
    },
    
    running : {
        
        animation: 'running.glb'
    },
    
    dance :  {
        
        animation: 'dance.glb'
    },
    
    falling : {
        
        animation: 'falling.glb'
    }
    
};



// initialize code called once per entity
PlayerMovement.prototype.initialize = function() {

    
    //rychlost prechodu animacie
    this.blendTime = 0.2;
    //nastavenie animacie: 'necinny'
    this.setState('idle');
    //boolean rozhodujuci o ne/spusteni animacie
    this.runAnimation = true;
    //podmienka ktora zistuje ci hrac je vo vzduchu alebo nie
    this.notFalling = true;
    //nastavenie animacii hraca po dopadnuti na objekt
    this.setIdleAfterFall = 1;
    //waypoint poradove cislo
    this.Point = 1;
    //vytvorenie noveho Vektoru3D
    this.vec = new pc.Vec3();
    //prechod ne/fyziky
    this.switchToPhysics = false;
    
    // --- variables
    this.targets = new pc.Vec3();
    this.tempvec3 = new pc.Vec3();
    this.temp2vec3 = new pc.Vec3();
    
};

// update code called every frame
PlayerMovement.prototype.update = function(dt) {
     console.log(this.lookTo);
    
    this.smoothLookAt();
    this.followPath(true);
    this.setFallingAnimation();
    this.setAfterFallAnimation();
    
    //Ziskavanie aktualnej linearnej x/y pozicie(!=0 {player v pohybe})
    var currentX = this.entity.rigidbody.linearVelocity.x;
    var currentY = this.entity.rigidbody.linearVelocity.y;
    var forward = this.entity.forward.clone().scale(playerSpeed);
    
    //Ovladanie hraca
    if(this.app.keyboard.wasPressed(pc.KEY_UP)&&this.runAnimation){
        
        this.setState('running');
    }
    if(this.app.keyboard.isPressed(pc.KEY_UP)){
        if(!this.switchToPhysics){
            this.entity.translateLocal(0,0,-2*dt);
        }else{
            this.entity.rigidbody.applyForce(forward);
            // playerSpeed += dt;  
            this.entity.rigidbody.linearVelocity = new pc.Vec3(currentX,currentY, this.playerMaxVelocitySpeed);
         }
    }
    
    if(this.app.keyboard.wasReleased(pc.KEY_UP)){
        if(this.switchToPhysics){
           this.playerStopMovement(true);
        }
        if(this.runAnimation){
        
        this.setState('idle');
        } 
    }
    
    //konci tu
};

//vytvorenie funkcie setState pre ulahcenie nastavenia animacii v kode
PlayerMovement.prototype.setState = function(state){
  
    var states = PlayerMovement.states;
    
    this.state = state;
    
    this.entity.findByName('Model').animation.play(states[state].animation, this.blendTime);
    
    
    
};
//funkcia pre zastavenie movementu hraca
PlayerMovement.prototype.playerStopMovement = function(boolean){
    this.boolean = boolean;
    if(boolean){
    this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    this.entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
    }
};

PlayerMovement.prototype.setFallingAnimation = function(){
    //ak sa hrac nachadza vo vzduchu tak sa nastavi falling animacia
    if(this.fallAnimation){
    if(this.entity.rigidbody.linearVelocity.y < -0.1){
        this.notFalling = false;
        this.runAnimation = false;
        this.setState('falling');
        this.setIdleAfterFall = 1;
    
    }
  }
};

PlayerMovement.prototype.setAfterFallAnimation = function(setBool){
    //nastavenie spat 'idle' animaciu po dopadnuti na zem
    if(this.entity.rigidbody.linearVelocity.y > -0.1){
        if(this.setIdleAfterFall===1){
            this.setState('idle');
            this.notFalling = true;
            this.runAnimation = true;
            this.setIdleAfterFall = 0;
            
        }
    }
    
};

PlayerMovement.prototype.followPath = function(setBool){
   this.setBool = setBool;
    if(this.setBool){
    this.point = this.app.root.findByName('Point ' + this.Point.toString()).getLocalPosition();
    var playerPosi = this.entity.getLocalPosition();
    var distance = this.point.distance(playerPosi);
    //this.temp2vec3.copy(this.targ.getLocalPosition());
  // var t = this;
    /*var newtween = this.app.tween(this.tempvec3).to(this.temp2vec3,2,pc.BounceInOut).on('update',function(){
            t.entity.lookAt(t.temp2vec3);
    }.bind(this)).start();*/
    
     this.entity.lookAt(this.point);
    
    this.Point = pc.math.clamp(this.Point,1,this.maxWayPoints);
    

 if(distance < 0.1){
   
   this.Point++;
 }
        
    }
    
};

PlayerMovement.prototype.smoothLookAt = function(){
    // --- stop any active tween
        if( this.tween ){
            this.tween.stop();
            this.tween = undefined;
        }
   // this.targets = this.app.root.findByName('Point '+ this.Point.toString());
    // --- initialize the tween with a new target
        this.lookTo.copy(this.targets[this.targetIndex].getPosition());
        
        this.tween = this.app
        .tween(this.lookFrom)
        .to(this.lookTo, 2, pc.SineInOut)
        .on('update', function () {

            this.entity.lookAt(this.lookFrom);

        }.bind(this) )
        .start();
        
        this.targetIndex = this.targetIndex === 0 ? 1 : 0;
    
};




// swap method called for script hot-reloading
// inherit your script state here
// PlyerMovement.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

I don’t see the line that I suggested in the initialize function.

oh noe its not here