Moving entity towards the cursor

Hello!

I am attempting to create a flight simulator. Currently, my Airplane can fly around a bit, but I’m looking to make it climb and dive slowly according to the cursor location on the screen.
My hope is to make it move towards that height slowly, to keep it as realistic as possible.

Any help would be deeply appreciated!

Here is my code:

var Test = pc.createScript('test');
// Initialize code called once per entity

// Update code called every frame
Test.prototype.update = function(dt) {
    var forwardForce = this.entity.forward.clone();
    forwardForce.scale(-10);
    var playerPos = this.entity.getPosition();
    
    // Sets the forward force values
    if (this.app.keyboard.isPressed(pc.KEY_W)){
        forwardForce.scale(5);
    }
    else{
        forwardForce.scale(0);
    }
    // Sets sideways force values
    var sideForce = this.entity.right.clone();
    if (this.app.keyboard.isPressed(pc.KEY_A)){
        this.entity.rigidbody.applyTorque(this.entity.forward.scale(2));
        sideForce.scale(5);
    }
    else if (this.app.keyboard.isPressed(pc.KEY_D)){
        this.entity.rigidbody.applyTorque(this.entity.forward.scale(-2));
        sideForce.scale(-5);
    }
    else{
        sideForce.scale(0);
    }
    // Applies the sideways and forward force values that have been set using the above if statements
    this.entity.rigidbody.applyForce(forwardForce);
    this.entity.rigidbody.applyForce(sideForce);
};

Hi @RumaiIndustries!

I’m not exactly sure what you are looking for, but maybe the example project below can help you.

https://playcanvas.com/project/808772/overview/look-at-with-physics

Thank you @Albertos, will take a look at this!

I believe I can use this, so thank you!
One last thing, while I’m still here. I’m also looking to play an animation only once when a key is clicked, do you mind giving me some advice?

Again, thank you so much for your help, you’re truly wonderful! :smile:

Thank you! You’re welcome!

Can you ask this in a new topic please? (Otherwise topics can get confusing).

Yes, of course!