Thursday, February 27, 2025
spot_img

Difficulty with Positioning and Motion in Cocos Creator 3.8 – Cocos Creator


I’ve encountered a difficulty in model 3.8. Beforehand, we might set the x place with out altering the entire vector in v2, however that’s not doable. Now, we’ve got to make use of one thing like this:

this.node.setPosition(new Vec3(this.xSpeed, 0, 0).add(this.node.getPosition()));

I used to be testing this Cocos Creator tutorial and tried so as to add motion utilizing the replace methodology and leaping utilizing tween. Nonetheless, as a result of challenge talked about above, I couldn’t get it to work. Right here’s the code I used:

For Leaping

let nodePos = this.node.place;
let jumpUp = tween(this.node).by(this.jumpDuration, { place: new Vec3(nodePos.x, this.jumpHeight, nodePos.z) }, { easing: 'sineOut' });
let jumpDown = tween(this.node).by(this.jumpDuration, { place: new Vec3(nodePos.x, -this.jumpHeight, nodePos.z) }, { easing: 'sineIn' });

let jumpTween = tween(this.node).sequence(jumpUp, jumpDown);
var jumpAction = tween(this.node).repeatForever(jumpTween);
tween(this.node).then(jumpAction).begin();

For Shifting Left and Proper

replace(dt: quantity) {
    if (this.accLeft)
        this.xSpeed -= this.accel * dt;
    else if (this.accRight)
        this.xSpeed += this.accel * dt;
    
    if (Math.abs(this.xSpeed) > this.maxMoveSpeed)
        this.xSpeed = this.maxMoveSpeed;

    this.node.setPosition(new Vec3(this.xSpeed, 0, 0).add(this.node.getPosition()));
}

The problem is that I can’t merely set the x place with out updating the entire vector, which complicates the implementation. Any solutions on easy methods to deal with this within the new model?


you alter the node place x in Leaping and alter it on the identical time in Shifting Left and Proper. So it cannot work out.

How do you suppose I ought to modify the code to repair this?

it’s not possible. there is no such thing as a strategy to make a node present two completely different place x on the identical time.

This was doable beforehand, proper? We might simply tween within the y route and transfer within the x route. There should be another for this. What I want is:

  1. Transfer the node repeatedly within the y route for the bounce motion.
  2. Transfer the node alongside the x axis when a button is clicked.

How do you suppose I can obtain this?

out of your description, you may make an animation up and down from animation clip. then use code to alter x axis when a button is clicked. that’s the method

Tried it, I added bounce to animation and set it to play on load together with loop as wrap mode. Motion has been stored as identical nevertheless it doesn’t work. It solely begins shifting if I take away animation.

One other methodology you possibly can strive:

this.nodePos = {y:1};
let jumpUp = tween(nodePos ).by(this.jumpDuration, { y:this.jumpHeight }, { easing: 'sineOut' });
let jumpDown = tween(nodePos).by(this.jumpDuration, { y: -this.jumpHeight }, { easing: 'sineIn' });

let jumpTween = tween(this.node).sequence(jumpUp, jumpDown);
var jumpAction = tween(this.node).repeatForever(jumpTween);
tween(this.node).then(jumpAction).begin();

after which within the replace Perform you should use this.nodePos, to set node place Y.

Thanks. This answer seems to be working, nevertheless it appears overly complicated. Is there a extra environment friendly or streamlined strategy?

I consider that what you need to obtain is a lerp logic contained in the replace cycle, however you are attempting to make use of Tween to attain this. Probably the most streamlined strategy to obtain it will be certainly centralize the motion logic within the replace cycle and add the leaping animation utilizing AnimationClip or associated.

You may check out this code right here to get an concept. You in all probability want an easier code than that to your logic.

        // Create a brand new motion vector primarily based on the character's enter
        Vec3.lerp(this.playerVelocity, this.playerVelocity, desiredVelocity, this.linearDamping * deltaTime);
        this.motion = Vec3.multiplyScalar(this.motion, this.playerVelocity, deltaTime);
        this.characterCtrl.transfer(this.motion);

And concerning the Tween API itself, it is advisable use parallel to replace it, not sequence, on this particular case.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles