Switch camera on Keypress

Hi I’m new here. I was making a game where I have the requirement to get in the car and get out. While I am able to get in but I cannot get my character to get out

Here is my code:

if(collision!=true){
                                       // here it adds the wasd input to the player velocity when the player is not in the car
					impostor.setLinearVelocity(new BABYLON.Vector3(x, velocity3d.y, z))
				}
				else if(isPressed("f")&& collision==true)  {
 // when the player collides with the tigger box and the user presses "f" , camera changes to camera1 giving the effect of him getting in the car , I also remove the player mesh and he cant move the player as it is only when collision!=true ( first condtion)
					scene.activeCamera=camera1;
					mesh.isVisible=false;
					collision=false;
					incar=true;
					console.log("in car now")
					
				}else if(scene.activeCamera==camera1){
//but when the player is already in the car, the scene.aciveCamera is camera1 so I can just use that with isPressed(key:string) that is "f" and i change back the activeCamera to camera :D , if only if would work 

					console.log("getting out of car")
					scene.activeCamera=camera;
					mesh.isVisible=true
					impostor.setLinearVelocity(new BABYLON.Vector3(x, velocity3d.y, z))
					incar=false;
				}
			})

for some reason the last part work don’t work. :frowning:

Help please :pleading_face:

Could you make a repro in PG?

Did you mean a repository in Playground?
If you mean that , well I can try. I have never done that before. I’m its quite easy.

I’ll add the link in here after making it

Thx for the help , but i don’t think it is linked to chrome dev tools. :smiley:

Repro - is short from REPRODUCE.
It means to reproduce (repeat) your problem in Playground so everybody could help you without guessing about what your code is.

1 Like

its part of a larger project containing many files
but i can try and make a smaller prototype and try it on the playground

2 Likes

hey @labris,
So I have been tweaking something to make the minimum viable stuff to help you understand the problem as you asked me to do so.

Its very basic and it doesnt work very well , It seemed like i was making another whole project instead of a quick example

so ill try and make another one but in the mean time if you can identify and correct any mistakes or gaps in the code, you’re very welcome !

Thanks and Regards,
@lucifer

As far I see your example is working and wheels are rotating as needed

1 Like

@labris
thanks for the quick reply.

But, I think you’re missing the point I started the thread for. ( switch camera on keypress)
but yeah the rotate mesh thing isn’t working on my local thing ( and its vastly different from the one in the playground)

as it is taking too long for me to re create the thing in PG , I will be linking a video and code files ( on request/ if you want) so that you understand the problem better

the file upload limit is only 4MB
so i only could upload this much
also, you can check out our website :earth_africa: https://benevolent.games/
you’ll get the gist of what I’m talking about.
but fortunately this is enough to explain

I need to change the camera like I did here but back to the original
which is not happening
and also I need help making the car move but that’s for later ( I have seen almost every post you’ve and @raggar has posted)

Not having a Playground reproduction makes it much harder for the community to be able to take a look at your problem, so I recommend having a PG if you’d still like help. It doesn’t need to be your entire game and use your assets, the simplest the better.

All I can see from this small part of code is that it’s based on collision and is also not returning a value for ‘incar’.
Are you sure ‘incar’ is actually set for your second part of script and/or collision is not blocking the script? No offense, I’m just asking, since this has been going on for a while. Sometimes the answer is just so simple that you cannot see it anymore.

1 Like

well it could be , but I’m not sure on what you mean.

well, you have two parameters in there that will decide on whether the function will trigger or not:
The collision and the ‘incar’ var. I believe first would be try determine if either of these is blocking and in case, which one is.
It is possible that the var ‘incar’ is not updated at the very time you interact again with the collision box. It’s also possible that the collision box (in or out) is not detected at the very time you call the ‘incar’ var.
For debug, I guess I would want to remove one of these triggers and check it. Next, remove the other and check it. Once I’m sure they all work independantly, reinstate both and if still buggy, I would know that the issue is with the order/combination of both.

Edit: May be add something like this
(impostor)…onCollideEvent = function(collided) {return incar;}

1 Like

Thanks, I’ll try that.