Hello everyone!
I started to play with PlayCanvas and so far I love it. Came from Unity3D (like running away from hell, it doesn’t support UnityScript anymore) and glad to be part of the web developers group again.
I’m in the process of developing a VR game and got stuck with the AI of the enemies. I think what I’m not understanding here is the way JavaScript can (or cannot) wait X seconds in another thread while continuing the main one.
I’m also having big problems understanding how to move/rotate an entity:
- Translate/Rotate are for non rigidbodies, right?
- ApplyForce/ApplyTorque are for rigidbodies, right?
- For the life of me I cannot deduce how to create a vector representing a random direction to walk on. Do I need to use ApplyForce with pc.Vec3.FORWARD to walk on the X axis, multiplied with a const speed and the deltatime? How can I align my model to that direction? Cause it seems to be walking backwards instead of forwards
- If I ApplyTorque to my entity it spins and accelerates nonstop in matter of seconds, even when multiplying with deltatime. But again, Rotate is not an option here, right?
- Is wrong to have an empty entity with a colission capsule, rigidbody and the AI script, and a child entity with the model, the textures and the animations? This way I can align better the model to the collision capsule, and separate the concepts for better understading.
- How can I know if the enemy is seeing me? Raycasting and checking if the ray collides with me but not with the terrain, I guess?
- How can I know the vector which represents the direction from where my enemy should move to reach me? I’m a bit a n00b in mathematics and vectors here, sorry.
- How can I wait X seconds for something to happen? I tried the setTimeout concept but I kind of miss an async/await implementation here. Let’s say I want to rotate during X seconds, and after that, continue my coroutine. Can I do that with a single thread? In Unity3D we had some sort of async coroutine which could idle for a few seconds before continuing.
Maybe I’m explaining myself a bit bad, and maybe (surely) I’m mistaken and it simply cannot be done, but so far the concept of what I’m achieving (based on really simple Unity3D tutorials like https://www.youtube.com/watch?v=aEPSuGlcTUQ) to do is the following (in pseudocode):
// initialize function
initialize () {
this.isTurningLeft = false
this.isTurningRight = false
this.isWandering = false
this.isMoving = false
this.isWaiting = false
this.moveSpeed = 3
this.rotationSpeed = 100
}
// update function
update (dt) {
if (!this.isWandering) this.wander()
if (this.isRotatingRight) this.entity.rigidbody.applyTorque(pc.Vec3.RIGHT * this.rotationSpeed * dt)
if (this.isRotatingLeft) this.entity.rigidbody.applyTorque(pc.Vec3.LEFT* this.rotationSpeed * dt)
if (this.isWalking) this.entity.rigidbody.applyForce(pc.Vec3.FORWARD * this.moveSpeed * dt)
}
// wander function, ideally async
wander () {
rotTime = getRandomInt(1, 5)
rotWait = getRandomInt(1, 5)
rotLR = getRandomInt(1, 2) // rotating Left or Right
walkWait = getRandomInt(1, 5)
walkTime = getRandomInt(1, 5)
this.isWandering = true
await walkWait * 1000 // <- wait some seconds, how can achieve that? setTimeout?
this.isWalking = true
await walkTime * 1000 // <- wait some seconds, how can achieve that? setTimeout?
if (rotLR === 1) {
this.isRotatingRight = true
await rotTime * 1000 // <- wait some seconds, how can achieve that? setTimeout?
this.isRotatingRight = false
}
if (rotLR === 2) {
this.isRotatingLeft = true
await rotTime * 1000 // <- wait some seconds, how can achieve that? setTimeout?
this.isRotatingLeft = false
}
this.isWandering = false
}
Can you give me some insight on this subject?
Thanks in advance and congratulations on such a great (and free!) piece of software.