Click and move with limits

because in some places it moves and in others it doesn’t? I would like to remove this limit, where am I losing? this code was already ready on playcanvas

PointAndClick.prototype.doRayCast = function (screenPosition) {
    
    var ray = PointAndClick.ray;
    var hitPosition = PointAndClick.hitPosition;

    this.cameraEntity.camera.screenToWorld(screenPosition.x, screenPosition.y, this.cameraEntity.camera.farClip, ray.direction); 
    ray.origin.copy(this.cameraEntity.getPosition());
    ray.direction.sub(ray.origin).normalize();
    
    // Test the ray against the ground
    var result = this.groundShape.intersectsRay(ray, hitPosition);  
    if (result) {
        this.movePlayerTo(hitPosition);
    }
    
   
};

hellppp mee

I guess the bounding box size you are setting is not big enough to cover the room. Increase the size of this.groundShape and it should work.

2 Likes

thanks, It worked

is it possible to lock the rotation of the x and z axis? lol lol lol look at this.

Yes, may be you have to make some changes in the movePlayerTo Function.

man, I have tried so far to no avail :sleepy:

var PointAndClick = pc.createScript('pointAndClick');

PointAndClick.attributes.add('cameraEntity', {type: 'entity', title: 'Camera Entity'});
PointAndClick.attributes.add('cameraEntityTarget', {type: 'entity', title: 'Camera Entity Target'});
PointAndClick.attributes.add('playerEntity', {type: 'entity', title: 'Player Entity'});
PointAndClick.attributes.add('playerSpeed', {type: 'number', default: 0, title: 'Player Speed'});
PointAndClick.attributes.add('status', {type: 'boolean', title: 'Status'});

// initialize code called once per entity
PointAndClick.prototype.initialize = function() {
    this.groundShape = new pc.BoundingBox(new pc.Vec3(0, 0, 0), new pc.Vec3(4, 0.001, 4));
    this.direction = new pc.Vec3();
    this.distanceToTravel = 0;
    this.targetPosition = new pc.Vec3();
    
    // Register the mouse down and touch start event so we know when the user has clicked
    this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);

    if (this.app.touch) {
        this.app.touch.on(pc.EVENT_TOUCHSTART, this.onTouchStart, this);
    }
    this.animation      = pc.app.root.findByName('player_allan_mash').script.personAnimate;
    this.player_mash    = pc.app.root.findByName('player_allan');
    this.walk           = false;
    this.targetPosition = new pc.Vec3 (0, 0, 0);
    this.lerpedPosition = new pc.Vec3 ();
    this.speed          = 3;
    this.lookFrom       = new pc.Vec3();
    this.lookTo         = new pc.Vec3();
    this.playerEntity.lookAt(this.lookFrom);
    this.movePlayerTo(this.lookFrom);
    
    this.rotation  = new pc.Vec3();
    
};


PointAndClick.newPosition = new pc.Vec3();

// update code called every frame
PointAndClick.prototype.update = function(dt) {
    
    
    if (this.direction.lengthSq() > 0) {
        
       
        
        if(this.walk===false){
           this.animation.setState('walk');
           this.walk = true;
        }
        
        // Move in the direction at a set speed
        var d = this.playerSpeed * dt;
        var newPosition = PointAndClick.newPosition;
       
        newPosition.copy(this.direction).scale(d);
        newPosition.add(this.playerEntity.getPosition());
        
        this.playerEntity.rigidbody.teleport(newPosition);     
        
        this.distanceToTravel -= d;
        
        // If we have reached our destination, clamp the position 
        // and reset the direction
        if (this.distanceToTravel <= 0) {
           this.direction.set(0, 0, 0);
           this.animation.setState('idle');
           this.walk = false;
           this.tween.stop();
           this.tween = undefined;
        }
        
         
   }
   
   this.lerpedPosition.lerp (this.cameraEntityTarget.getPosition(), PointAndClick.newPosition, this.speed * dt);

   this.cameraEntityTarget.setPosition(this.lerpedPosition); 
    
   
};


PointAndClick.prototype.movePlayerTo = function (worldPosition) {
    this.targetPosition.copy(worldPosition);
        
    // Assuming we are travelling on a flat, horizontal surface, we make the Y the same
    // as the player
    this.targetPosition.y = this.playerEntity.getPosition().y;

    this.distanceTravelled = 0;
    
    // Work out the direction that the player needs to travel in
    this.direction.sub2(this.targetPosition, this.playerEntity.getPosition());
    
    // Get the distance the player needs to travel for
    this.distanceToTravel = this.direction.length();
    
    if (this.distanceToTravel > 0) {
        // Ensure the direction is a unit vector
        this.direction.normalize();
        
        if( this.tween ){
            this.tween.stop();
            this.tween = undefined;
        }
    
        this.lookTo = this.targetPosition;
        //this.lookTo.x = this.playerEntity.getPosition().x;
        //this.lookTo.z = this.playerEntity.getPosition().z;


        this.tween = this.app
            .tween(this.lookFrom)
            .to(this.lookTo, 0.32, pc.Linear)
            .on('update', function () {
               //this.playerEntity.setRotation(0,this.playerEntity.getRotation(),0);
                this.playerEntity.lookAt(this.lookFrom);
                

            }.bind(this))
            .start();
            //this.lookFrom = this.playerEntity.getPosition();
            //this.playerEntity.lookAt(this.targetPosition);
            //
 
    } else {
        this.direction.set(0, 0, 0);
    }
    
    
};


PointAndClick.prototype.onMouseDown = function(event) {
    if (event.button == pc.MOUSEBUTTON_LEFT && this.status===true) {
        this.doRayCast(event);
    }
};


PointAndClick.prototype.onTouchStart = function (event) {
    // On perform the raycast logic if the user has one finger on the screen
    if (event.touches.length == 1  && this.status===true) {
        this.doRayCast(event.touches[0]);
        event.event.preventDefault();
    }    
};


PointAndClick.ray = new pc.Ray();
PointAndClick.hitPosition = new pc.Vec3();

PointAndClick.prototype.doRayCast = function (screenPosition) {
    var ray = PointAndClick.ray;
    var hitPosition = PointAndClick.hitPosition;
    this.groundShape = new pc.BoundingBox(new pc.Vec3(0, 0, 0), new pc.Vec3(1000, 0, 1000));
    this.cameraEntity.camera.screenToWorld(screenPosition.x, screenPosition.y, this.cameraEntity.camera.farClip, ray.direction); 
    ray.origin.copy(this.cameraEntity.getPosition());
    ray.direction.sub(ray.origin).normalize();
    
    
    // Test the ray against the ground
    var result = this.groundShape.intersectsRay(ray, hitPosition);  
    if (result) {
        this.movePlayerTo(hitPosition);
    }
    
   
};

YEA :slight_smile:
this.lookFrom.y = this.playerEntity.getPosition().y;