-
Notifications
You must be signed in to change notification settings - Fork 15
The camera position is not updated in real time. #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I will take a look. This may be because the OrbitControls takes control and overrides the camera position |
In the example: |
So the behaviour your seeing is because the To let React manage the position again, you can disable the script by setting its <Entity>
<Camera />
{ enabledOrbitControls ? <OrbitControls /> : null }
</Entity> Also worth noting — the playground example linked in the issue is a bit of a special case because it re-renders everything on each change, which can mask this kind of interaction. In a typical app, the Script’s internal logic can persist across renders unless explicitly disabled. |
The buttons already update the camera position in real time with setCamPos(coords), but how can I achieve camera transitions between different positions? |
There are a couple of ways you could achieve this — either using a Script, or something more React-based. Animating with <Script/>You can create a new Script that transitions an entity using the PlayCanvas engine API. import { Script, Vec3 } from 'playcanvas'
class Transition extends Script {
// Sets the target position
target = [0, 0, 0]
// Vec3 representation of the target position
targetVec3 = new Vec3(0, 0, 0)
// Vec3 to hold the interpolated (lerped) position
lerpedPosition = new Vec3()
// How fast the entity moves towards the target
speed = 1
update(dt) {
this.targetVec3.fromArray(this.target)
// Lerp from the current position to the target position
this.lerpedPosition.lerp(this.entity.getPosition(), this.targetVec3, this.speed * dt)
// Update the entity's position
this.entity.setPosition(this.lerpedPosition)
}
}
// In your JSX
<Entity>
<Camera />
{enabledOrbitControls ? (
<OrbitControls /> // Use OrbitControls when enabled
) : (
<Script script={Transition} target={[0, 10, 0]} speed={2} /> // Otherwise, use the Transition Script
)}
</Entity> Animating with framermotion/reactspringAlternatively you could use something like Check the animation example for a demo. It's using a |
This is what I want to recreate: But it seems that a camera entity does not behave the same as a normal entity, and I notice many type errors, is this common? |
I try this but didnt work |
I managed to move the camera away from the origin, but not with an animation, it would be good if the camera had the prop look at https://stackblitz.com/edit/playcanvas-react-template-u2wqujvx?file=src%2FScene.tsx |
I’ve taken a quick look — the key here is to disable the orbit controls while animating to a new location, and then re-enable them once the transition is complete. Here’s the basic idea: <Entity>
<Camera />
{isTransitioning ? (
<Script
script={TransitionToWayPoint}
location={location}
/>
) : (
<Script
script={CameraControls}
focusPoint={waypoint.position}
/>
)}
</Entity> I’ve put together a quick example here on StackBlitz. Let me know if you get stuck — happy to help refine it further! |
https://stackblitz.com/edit/playcanvas-react-template-yh7tyzth?file=src%2FApp.tsx I'm trying to switch between two different camera control scripts on the same camera entity using conditional rendering in React, like this:
When the transition script finishes (isTransitioning becomes false), my MyOrbitControls script mounts. This script needs an initial focusPoint (a pc.Vec3) from my React state so it knows what to orbit around. The issue is, I can't reliably get this initial focusPoint into the MyOrbitControls script when it mounts. What I've Tried (and didn't work):Passing focusPoint via args: Tried <Script script={MyOrbitControls} args={[{ focusPoint: myCorrectVec3 }]} />. Inside the script's initialize(), this.focusPoint (or the internal this._origin) always ends up being [0, 0, 0], not the value I passed. Logs confirm the correct value was calculated in React right before. Because MyOrbitControls never gets the right initial focusPoint, it starts up using incorrect data (like focusing on [0,0,0]). As soon as it takes control after the smooth TransitionScript finishes, it applies its own incorrectly calculated transform, causing a very noticeable visual jump/snap in the camera view. It doesn't smoothly take over from where the transition ended. My Guess:This feels like a timing or synchronization problem between React's conditional rendering/mounting lifecycle and how the @playcanvas/react <Script> component handles passing args or assigning the ref. It seems React can't reliably talk to the script instance right after it's conditionally mounted. Question:Is this a known issue or limitation when using conditionally rendered <Script> components, especially with ES6 class scripts? What's the recommended way with @playcanvas/react to get initial state from React into a script instance right when it mounts in a scenario like this? Or is using pc.createScript necessary for this kind of integration to work reliably? Thanks for any pointers! |
So <Script script={YourScript} speed={10} /> This sets the speed prop which is available in the initialize as It's worth checking if you're getting any warning in dev tools for invalid props. We've got pretty good coverage for validating things |
Just a heads up @Alecapra96 - script refs are now correctly forwarded on. See #129 |
If I change the position of <Entity name="camera" position={[11, 0, 2]}> It doesn't take effect until I refresh the page.
The solution I found is:
But it's not the best, and if I want to make animations with the camera or transitions from front to back, I don't know how to do it.
Dependencies :
"dependencies": {
"@playcanvas/react": "^0.3.0",
"@preact/preset-vite": "^2.10.1",
"@tailwindcss/vite": "^4.1.4",
"lucide-react": "^0.475.0",
"playcanvas": "2.3.3",
"preact": "^10.26.5",
"tailwindcss": "^4.1.4"
},
BTW playanvas/react 0.3 dont work with the last of playcanvas
The text was updated successfully, but these errors were encountered: