[SOLVED] About touch controls on third person controller

Hi I’m doing third person touch moving project using PlayCanvas.

I am learning with this project :
https://playcanvas.com/project/721980/overview/third-person-controller

And I found error will showing when I don’t using touch to play it.
Error was about this.app.touch.on

I know if I play this project with keyboard, app.touch.on will be null and cause this error.
But I really don’t know how to fix it…

I put app.on codes inside if (this.app.touch) {}
Project went well but mouse move(Camera movement) don’t work anymore.

So… finally what I want to do is :
keyboard user can play with keyboard
and mobile user can play with touch.
DO NOT NEED TO SWITCH.(If can, will be better)

Really need help please.

With touch, you will have to check if it’s available. eg

if (this.app.touch) {
    this.app.touch.on(pc.EVENT_TOUCHSTART, this.onTouchStart, this);
    this.app.touch.on(pc.EVENT_TOUCHEND, this.onTouchEndCancel, this);
    this.app.touch.on(pc.EVENT_TOUCHCANCEL, this.onTouchEndCancel, this);
    this.app.touch.on(pc.EVENT_TOUCHMOVE, this.onTouchMove, this);
}

YES I have done this code. But It cause an other ERROR. That is my point.

I’ve just forked it with the code change I mentioned above and can’t see the issue?
https://playcanvas.com/editor/scene/1105769

Try to play with keyboard and mouse. Camera will not spin while mouse is moving. If you disable touchCameraMovement it will moving fine. That is the issue.

In regards to making it work with both mouse and touch:

Right now, you have two separate scripts controlling the same entity every update.

eg

TouchCameraMovement.prototype.postUpdate = function(dt) {
    var originEntity = this.entity.parent;

    var targetY = this.eulers.x + 180;
    var targetX = this.eulers.y;

    var targetAng = new pc.Vec3(-targetX, targetY, 0);

    originEntity.setEulerAngles(targetAng);

    this.entity.setPosition(this.getWorldPoint());

    this.entity.lookAt(originEntity.getPosition());
};

What needs to be done is to allow for touch or mouse to be used to change the angles on the same script.

Nice adv. But I have change this.eulers in Touch codes to this.toucheulers to make it independent. But also not working…

I’ve quickly made the project so that the camera movement can work with both touch and mouse as an example:

https://playcanvas.com/project/772152/overview/forums-third-person-controler

Code here: https://playcanvas.com/editor/code/772152?tabs=43784422

More question please.
What is difference between prototype.update and prototype.postUpdate ?

Post update is called after all the Updates are called.

ie All script updates are called, then all script postUpdates are called

Oh… I have tested your project. Character seems not moving while using touch (leftstick)

Yes, I only did the camera movement as example on how to have both the touch and mouse controls on the same script.

Thanks for helping. I will try the rest.
But If you can make it perfect. It will be the best sample project with third-person both touch and keyboard playing.