[SOLVED] What am I doing wrong?

Hello, all.
I am working with the character controller script that you can download from the asset store.

I added collision detection and then put this code:

CCScript.prototype.onCollisionStart = function (result){
   if (result.other.tags.has('toSeg2')) {
this.segment2.enabled = true;
this.segment1.enabled = false;
this.entity.setPosition(-3.5,0.5,0);
this.controller.look.y = -90; 
console.log('Segment 2');
};
   if (result.other.tags.has('toSeg1')) {
this.segment1.enabled = true;
this.segment2.enabled = false;
this.entity.setPosition(3.5,this.entity.getPosition().y,this.entity.getPosition().z);
this.controller.look.y = 90;
console.log('Segment 1');
};
};

The problem is on line 5. For some reason when I set the position to -3.5, it goes to 3.5. I don’t know why this happens, but I really need this to work. Thanks for any help!

Hey, it’s difficult to tell with the code. Can you make a sample project where bug can debugged?

2 Likes

Here is the project. PlayCanvas 3D HTML5 Game Engine
Here is a quick rundown of the project:
You start on a platform with a 1 below your character, if you go towards the 2 it will disable the current platform and enable another one with a 2 on the platform.
When the platforms enable and disable the character should teleport to one side, -3.5,0.5,0.
(I put a red cube at (3.5,0.5,0) and a blue one at (-3.5,0.5,0) for reference).
The idea is to make an illusion that you are going around a planet.

I know that’s a lot of info so if I can help break anything down I will be happy to help
Thanks!

you are using Dynamic rigidbody for the player.

So it won’t move with setPosition function. You will have teleport it using rigidbody Teleport function in order to move the player.
Simply update setPosition line with this.entity.rigidbody.teleport(yourposition) and it will work.
Here is the updated code:

CCScript.prototype.onCollisionStart = function (result){
   if (result.other.tags.has('toSeg2')) {
this.segment2.enabled = true;
this.segment1.enabled = false;
this.entity.rigidbody.teleport(-3.5,0.5,0);
this.controller.look.y = -90; 
console.log('Segment 2');
};
   if (result.other.tags.has('toSeg1')) {
this.segment1.enabled = true;
this.segment2.enabled = false;
this.entity.rigidbody.teleport(3.5,this.entity.getPosition().y,this.entity.getPosition().z);
this.controller.look.y = 90;
console.log('Segment 1');
};
};

To study about rigidbody, you can refer to documentation

Thanks! I did not know that.

Seems I spoke too soon

When I touch the collision thing, it starts teleporting me between the 2 points. I tried to get a video but it wouldn’t work.

Isn’t this as expected?

In rapid succession so I cant move. If you try the project it will happen.

That’s likely because you teleporting into another trigger so it’s onTrigger callback is called

1 Like

@yaustar Thanks, it made me think about the size of the player. I thought it wouldn’t teleport into the collision because it teleported to -3.5 but the wall was at 4. I just had to change the collision to 5.