# \[SOLVED\] Raycast(+physics) not working in engine-only (v1.67.3)

**URL:** <https://forum.playcanvas.com/t/solved-raycast-physics-not-working-in-engine-only-v1-67-3/34333>\
**Category:** Help & Support\
**Tags:** physics, engine-only\
**Created:** [December 21, 2023, 6:10pm UTC](https://forum.playcanvas.com/t/solved-raycast-physics-not-working-in-engine-only-v1-67-3/34333 "2023-12-21T18:10:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slynch](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/slynch/32/29462_2.png) [@Slynch](https://forum.playcanvas.com/u/Slynch)\
**Post date:** [December 21, 2023, 6:10pm UTC](https://forum.playcanvas.com/t/solved-raycast-physics-not-working-in-engine-only-v1-67-3/34333/1 "2023-12-21T18:10:17Z")

</div>

I’ve been migrating my framework backbone from ThreeJS to PlayCanvas, and I’m encountering issues in implementing physics/raycast functionality. Specifically, raycast functions do not return any results (`[]`) when they visibly should, and gravity does not seem to be acting on dynamic/kinematic objects.

The following usual culprits have already been checked: Ammo is loaded and fetched from WasmModule before initializing the AppBase with the RigidBody/Collision component systems; the relevant entities definitely possess the components; and in the case of raycasting, the ray definitely passes through the object. Infact, the code is almost identical to the raycast code example provided by PlayCanvas, but simply does not work.

A live example can be found here (with sourcemap): [https://slynch.xyz/playcanvas\_physics/](https://slynch.xyz/playcanvas_physics/)  
The relevant file is `PlayCanvasRaycastTestState.ts`, as well as `Engine.ts` if you wish to inspect the AppBase initialization et al.

I’ll provide snippets on request, since as I said the ‘scene’ code is almost verbatim from the raycast example, and it’d be too nebulous to attach all the engine initialization code.

I’m convinced it’s something as simple as I’m not calling some physics update loop that is handled automatically in the editor version of PlayCanvas, but I can’t seem to find it myself nor in the documentation.

UPDATE: If anyone else has this issue, try ensuring that your `AppBase.start()` call occurs before creating physics objects.

---

<div class="post-metadata">

**Author:** ![Albertos](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/albertos/32/19749_2.png) [@Albertos](https://forum.playcanvas.com/u/Albertos)\
**Post date:** [December 21, 2023, 6:56pm UTC](https://forum.playcanvas.com/t/solved-raycast-physics-not-working-in-engine-only-v1-67-3/34333/2 "2023-12-21T18:56:16Z")

</div>

Hi @Slynch and welcome!

Personally I have no engine-only experience, but perhaps the example page below can help you.

[https://playcanvas.github.io/#/physics/raycast](https://playcanvas.github.io/#/physics/raycast)

---

<div class="post-metadata">

**Author:** ![LeXXik](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/lexxik/32/6953_2.png) [@LeXXik](https://forum.playcanvas.com/u/LeXXik)\
**Post date:** [December 21, 2023, 11:36pm UTC](https://forum.playcanvas.com/t/solved-raycast-physics-not-working-in-engine-only-v1-67-3/34333/3 "2023-12-21T23:36:41Z")

</div>

Had a quick look. Not sure where the issue is, as it looks ok. The raycast definitely works in 1.67.3. The engine-only example in the repo is using that version.

The Editor doesn’t do anything about Ammo physics except importing it to your project. The physics world is stepped in the Rigidbody Component System, inside `onUpdate` method. The raycast would return nothing, if you’d never stepped the world. However, I do see the `onUpdate` runs every frame in your app, and the physics world is stepping fine. Must be something else.

Some notes:

- When testing, don’t use a moving ray. Hardcode start and end positions at vertical 10 and -10, going through dead center. Place body in world center. All this moving is not necessary and making it harder to debug.
- When adding components you are passing `active` property - it doesn’t do anything (unless you have your custom components). Also `enabled` is true by default, no need to pass it.
- Make the ball dynamic, and make sure it falls down.
- Use debug drawer to make sure the visual mesh is matching the rigidbody position:  
[PlayCanvas 3D HTML5 Game Engine](https://playcanvas.com/project/744419/overview/ammo-debug-draw)

---

<div class="post-metadata">

**Author:** ![Slynch](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/slynch/32/29462_2.png) [@Slynch](https://forum.playcanvas.com/u/Slynch)\
**Post date:** [December 22, 2023, 3:40pm UTC](https://forum.playcanvas.com/t/solved-raycast-physics-not-working-in-engine-only-v1-67-3/34333/4 "2023-12-22T15:40:19Z")

</div>

Thanks for the reply. I tried hardcoding the ray with no movement and using your debug drawer code (works a charm!) but still the raycast function returns nothing. Additionally, setting the type to `RIGIDBODY_TYPE_DYNAMIC` does not cause the ball to fall (this was the main reason for my thinking that whatever physics step there is is not being called).

 ![bugged physics](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/3X/8/c/8c4f3c22fc0da73fd12fb827aaddb4da1daa4709.png)

I’m going to clone the PlayCanvas repo and step-through the entire raycast example from start to finish; maybe I can see what I’m missing from that.

(P.S. the redundant `active`/`enabled` properties are a by-product of everything I try not working!)

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [December 22, 2023, 3:47pm UTC](https://forum.playcanvas.com/t/solved-raycast-physics-not-working-in-engine-only-v1-67-3/34333/5 "2023-12-22T15:47:35Z")

</div>

You can step it here as well: [PlayCanvas Examples](https://playcanvas.github.io/#/physics/raycast)

- Add a debug line like I added on like 54 for example
- reload it (with the button on top left of the code window)
- in the chrome console, put breakpoint on the line printing log
- reload again (using the same button) - and you get a breakpoint hit.

 ![Screenshot 2023-12-22 at 15.45.57](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/3X/5/c/5cb138d2d94d5a3aade0c57e20a73a057d9f9b5c.jpeg)

---

<div class="post-metadata">

**Author:** ![Slynch](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/slynch/32/29462_2.png) [@Slynch](https://forum.playcanvas.com/u/Slynch)\
**Post date:** [December 22, 2023, 3:54pm UTC](https://forum.playcanvas.com/t/solved-raycast-physics-not-working-in-engine-only-v1-67-3/34333/6 "2023-12-22T15:54:34Z")

</div>

Found the issue! Apparently any physics objects created prior to calling `AppBase.start()` are (\*almost-)entirely ignored. Calling it at the start of my `onAwake` function rather than at the end immediately fixed both raycast + gravity issues.

Not sure how to proceed from here; I’d suggest either updating the `RigidBodyComponentSystem` documentation to note this behaviour (if it’s intended) or investigating the issue if not.

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [December 22, 2023, 4:45pm UTC](https://forum.playcanvas.com/t/solved-raycast-physics-not-working-in-engine-only-v1-67-3/34333/7 "2023-12-22T16:45:37Z")

</div>

Interesting find. If this is something that can be reproduced in the engine example I linked above, it’d be worth creating an engine issue - not that I’d expect it to be fixed quickly.

It smells like a bug to me … as typically engine objects can be created before the app stats. Not sure what the difference is here.

Glad the workaround is so easy though.

---

<div class="post-metadata">

**Author:** ![Slynch](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/slynch/32/29462_2.png) [@Slynch](https://forum.playcanvas.com/u/Slynch)\
**Post date:** [December 22, 2023, 5:10pm UTC](https://forum.playcanvas.com/t/solved-raycast-physics-not-working-in-engine-only-v1-67-3/34333/8 "2023-12-22T17:10:32Z")

</div>

Funnily enough, moving the `app.start()` call in the raycast example to after the physics objects are created causes a runtime error when adding the Rigidbody component (as opposed to my issue of it failing silently).

---

<div class="post-metadata">

**Author:** ![LeXXik](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/lexxik/32/6953_2.png) [@LeXXik](https://forum.playcanvas.com/u/LeXXik)\
**Post date:** [December 22, 2023, 5:51pm UTC](https://forum.playcanvas.com/t/solved-raycast-physics-not-working-in-engine-only-v1-67-3/34333/9 "2023-12-22T17:51:11Z")

</div>

Make sure you are not initializing the application twice. Maybe you are creating the body in a different instance.

---

<div class="post-metadata">

**Author:** ![Slynch](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/slynch/32/29462_2.png) [@Slynch](https://forum.playcanvas.com/u/Slynch)\
**Post date:** [December 24, 2023, 4:07pm UTC](https://forum.playcanvas.com/t/solved-raycast-physics-not-working-in-engine-only-v1-67-3/34333/10 "2023-12-24T16:07:34Z")

</div>

Not gonna lie, I balked at this at first, then thought “huh, actually…” 😂

But fortunately not; I double-checked and- at least in the live example I provided in the OP- the first and only `AppBase.start()` call is in my state’s `onAwake()` function. Additionally the `initialize` function is definitely not called multiple times.
