Attaching camera to a car and making it look realistic

For this student game, the camera is attached to a car. However, when you move the mouse down, the car disappears under the ground. It is on Scene 4. How do we prevent the car from doing this? Thanks!

https://edu.cospaces.io/ULF-FGX

Hi Kelly,

You’ve attached the car to the camera, not the other way round, I think. When attaching the camera to the car, the camera should become fixed position and moving the view with the mouse should not move the car.

You can then override the arrow/WASD keys, using event blocks, to move the car (with camera attached).

Hope that helps! Let me know if you have any further questions.

Geoff @ TechLeap

thanks Geoff (again)! It works.

one student is now trying to, when they click the dragon, to have the camera get on top of the dragon and ‘ride it’. How can we do that?

You can use the click event block and the detach/attach blocks to detach the camera from the car and attach it to the dragon :blush:

Hope that helps! Let me know if you have any further questions on this topic.

Geoff @ TechLeap

Hi Geoff,

I have asked this before, but still am confused by it. On Scene 2 of https://edu.cospaces.io/WHG-GJE, the car continues to drive through the walls. Physics is turned on with the car and the walls, and the car is attached to the camera. We feel as if we have tried everything and it still doesn’t ‘bounce’ off the wall. How could we fix this? Once again, thanks for your help!

Hi Kelly,

It’s a little hard to tell exactly what’s happening without remixing it and seeing inside, but here’s a few pointers:

  • When overriding keys with onkeypress events, all notions of grid boundaries disappear (unless you enforce them).
  • Anything outside the grid will not have physics applied.
  • If an object travels fast enough it may pass through any object with physics (due to movement occurring between frames)

Hope that helps! Let me know if you have any further questions.

Geoff @ TechLeap

Thanks Geoff! However, I’m not sure what you mean by this:

  • When overriding keys with onkeypress events, all notions of grid boundaries disappear (unless you enforce them).

Also, I assumed you could remix it and look inside.

I can’t remix the space unless you make it remixable :blush: Normally, when navigating a CoSpace, you cannot walk/fly outside of the grid (shown in the Editor) because there are grid boundaries which prevent this. The environment outside of the grid doesn’t have physics applied to it. I assume you are using when key is pressed events to control the car. When you use these events to control the car, the grid boundaries don’t apply, so your car can go outside of the grid.

Does that make sense?

Geoff @ TechLeap

Thanks Geoff,

We also have this issue. A student wants to have the boy ‘run’ and the camera to follow when he presses the W key. However, after 1 second the boy stops moving because we can’t get the camera to continue to follow him. This is in Scene 1 of https://edu.cospaces.io/WHG-GJE. You should be able to remix it. Any thoughts?

Hi Kelly,

Every time you make a change on a CoSpace, if you want to share that change, you need to click Share > Update to publish the changes. There are no CoBlocks in the CoSpace you shared, so I’m guessing this is what has happened.

However, I’m guessing you just want the camera to follow the boy? Simply attach the camera to the boy by dragging the Camera object onto the Boy in the Scene Inspector (list of objects in top-left tab) - this sets it as a child of the Boy.

Hope that helps! Let me know if you have any further questions.

Geoff @ TechLeap

This is the link: https://edu.cospaces.io/WHG-GJE

This is the script. But the camera WILL NOT move forward, even though it is attached to the boy.

https://edu.cospaces.io/WHG-GJE

This is the other game. In Scene 2, the car keeps driving through the wall. I believe you can now watch the game. https://edu.cospaces.io/WHG-GJE

Hi @Kelly_Nickell,

I’ve fixed Scene 1. This should tell your students all they need to know about the game mechanics they want to achieve (hopefully). Get them to Remix and compare. Notice the Physics I’ve added.

Hope that helps! Let me know if you have any further queries.

Geoff @ TechLeap

Thanks Geoff! But when I play it, when I turn to the left to go around the corner, the character doesn’t turn. The camera does.

Never mind! I didn’t know I had to press A and D for that to work.

It’s possible to disable the mouse interaction so that only keys can be used, by creating a script with the following code:

Input.onKeyPressed(function(){},‘left_mouse_button’);

Thanks Geoff,

In this game, in Scene 2, if you press B to get out of the directions, and press W to move the car, the car will still go through some walls.

https://edu.cospaces.io/WHG-GJE

I am sorry for all the questions, but you are the only person who can help us! Thanks!

Hi @Kelly_Nickell,

There are a couple of things to do and understand:

  1. To simplify things, I suggest putting a giant transparent cuboid around the buildings and turning on Physics for it.

  2. You can try adding an additional collision event to the above cuboids, so if the car collides with them, the car moves back slightly.

  3. Frame rate - if an object moves too fast it can move through other physics objects in between frames (depending on the GPU of your computer); therefore slowing down the car may help.

  4. Adding a restrict Car rotation CoBlock will help prevent the car flipping over; however it will not stop the car being pushed out of another object.

@Nikolay, do you have any other suggestions?

Geoff @ TechLeap

I would recommend to implement all of the above items.
You could also play with the mass, friction and forces to achieve best results (there is a simple example in this thread)

1 Like