There are a number of issues with this so let’s run through some of them:
var HeadBobController = pc.createScript('headBobController');
//.....
Vec3.prototype.FootStepMotion = function () {
Vec3.Zero = new Vec3(0, 0, 0);
pos.y += Mathf.Sin(Time.time * _frequency) * _amplitude;
pos.x += Mathf.Cos(Time.time * _frequency / 2) * _amplitude * 2;
return pos;
};
HeadBobController.prototype.ResetPosition = function () {
if (_camera.locaPosition == _startPos) return;
_camera.locaPosition = Vec3.Lerp(_camera.locaPosition, _startPos, 1 * Time.dt);
};
Vec3.prototype.FocusTarget = function () {
Vec3.Zero = new Vec3(transfrom.position.x, transform.position.y + _cameraHolder.locaPosition.y, transform.position.z);
pos += _cameraHolder.forward * 15;
return pos;
};
For some reason, you are using Vec3
prototype instead of HeadBobController
. Vec3
doesn’t exist in this scope and I assume you were trying to declare some functions for the HeadBobController
so let’s fix that first:
var HeadBobController = pc.createScript('headBobController');
HeadBobController.attributes.add('_enable', { type: 'boolean', default: true });
HeadBobController.attributes.add('_amplitudeStart', { type: 'number', default: 0.015, min: 0, max: 0.1 });
HeadBobController.attributes.add('_frequency', { type: 'number', default: 10, min: 0, max: 30 });
HeadBobController.attributes.add('_camera', { type: 'entity' });
HeadBobController.attributes.add('_cameraHolder', { type: 'entity' });
// initialize code called once per entity
HeadBobController.prototype.initialize = function () {
this._toggleSpeed = 3;
this._startPos = new pc.Vec3(0, 0, 0);
_startPos = _camera.locaPosition;
};
// update code called every frame
HeadBobController.prototype.update = function (dt) {
if (!_enable) return;
CheckMotion();
ResetPosition();
_camera.LookAt(FocusTarget());
};
HeadBobController.prototype.CheckMotion = function () {
this.speed = new pc.Vec3(_controller.velocity.x, 0, _controller.velocity.z).magnitude;
if (speed < _toggleSpeed) return;
if (!_controller.isGrounded) return;
PlayMotion(FootStepMotion());
};
HeadBobController.prototype.FootStepMotion = function () {
Vec3.Zero = new Vec3(0, 0, 0);
pos.y += Mathf.Sin(Time.time * _frequency) * _amplitude;
pos.x += Mathf.Cos(Time.time * _frequency / 2) * _amplitude * 2;
return pos;
};
HeadBobController.prototype.ResetPosition = function () {
if (_camera.locaPosition == _startPos) return;
_camera.locaPosition = Vec3.Lerp(_camera.locaPosition, _startPos, 1 * Time.dt);
};
HeadBobController.prototype.FocusTarget = function () {
Vec3.Zero = new Vec3(transfrom.position.x, transform.position.y + _cameraHolder.locaPosition.y, transform.position.z);
pos += _cameraHolder.forward * 15;
return pos;
};
In FootStepMotion
, it looks like you were trying to create a new pc.Vec3
and use it to calculate a new position based on time and cos/sin waves. However, Vec3
doesn’t exist as a variable so it’s going to give you an undefined error.
Also, you are trying to create a new Vec3
object from PlayCanvas which is in the pc
object. So again, that class doesn’t exist, you have to use pc.Vec3
. I suspect you want something like this:
HeadBobController.prototype.FootStepMotion = function () {
var pos = new pc.Vec3(0, 0, 0);
pos.y += Mathf.Sin(Time.time * _frequency) * _amplitude;
pos.x += Mathf.Cos(Time.time * _frequency / 2) * _amplitude * 2;
return pos;
};
Time
also doesn’t exist. You will need to track this yourself by creating a new class property and updating it in the update
call.
var HeadBobController = pc.createScript('headBobController');
HeadBobController.attributes.add('_enable', { type: 'boolean', default: true });
HeadBobController.attributes.add('_amplitudeStart', { type: 'number', default: 0.015, min: 0, max: 0.1 });
HeadBobController.attributes.add('_frequency', { type: 'number', default: 10, min: 0, max: 30 });
HeadBobController.attributes.add('_camera', { type: 'entity' });
HeadBobController.attributes.add('_cameraHolder', { type: 'entity' });
// initialize code called once per entity
HeadBobController.prototype.initialize = function () {
this._time = 0;
this._toggleSpeed = 3;
this._startPos = new pc.Vec3(0, 0, 0);
_startPos = _camera.locaPosition;
};
// update code called every frame
HeadBobController.prototype.update = function (dt) {
this._time += dt;
if (!_enable) return;
CheckMotion();
ResetPosition();
_camera.LookAt(FocusTarget());
};
HeadBobController.prototype.CheckMotion = function () {
this.speed = new pc.Vec3(_controller.velocity.x, 0, _controller.velocity.z).magnitude;
if (speed < _toggleSpeed) return;
if (!_controller.isGrounded) return;
PlayMotion(FootStepMotion());
};
HeadBobController.prototype.FootStepMotion = function () {
var pos = new pc.Vec3(0, 0, 0);
pos.y += Mathf.Sin(this._time * _frequency) * _amplitude;
pos.x += Mathf.Cos(this._time * _frequency / 2) * _amplitude * 2;
return pos;
};
HeadBobController.prototype.ResetPosition = function () {
if (_camera.locaPosition == _startPos) return;
_camera.locaPosition = Vec3.Lerp(_camera.locaPosition, _startPos, 1 * Time.dt);
};
HeadBobController.prototype.FocusTarget = function () {
var pos = new Vec3(transfrom.position.x, transform.position.y + _cameraHolder.locaPosition.y, transform.position.z);
pos += _cameraHolder.forward * 15;
return pos;
};
Mathf
doesn’t exist but I’m assuming you want to use the JS standard library math functions
HeadBobController.prototype.FootStepMotion = function () {
var pos = new pc.Vec3(0, 0, 0);
pos.y += Math.Sin(this._time * _frequency) * _amplitude;
pos.x += Math.Cos(this._time * _frequency / 2) * _amplitude * 2;
return pos;
};
You are also trying to call the functions you’ve created but you need to always reference the this
object when doing so as the functions belong to the ScriptType object. Right now, you are calling those functions as though they are in the global scope but they don’t exist and will give you an undefined error.
Same with accessing the scripttype properties.
Here’s a quick fix up:
var HeadBobController = pc.createScript('headBobController');
HeadBobController.attributes.add('_enable', { type: 'boolean', default: true });
HeadBobController.attributes.add('_amplitudeStart', { type: 'number', default: 0.015, min: 0, max: 0.1 });
HeadBobController.attributes.add('_frequency', { type: 'number', default: 10, min: 0, max: 30 });
HeadBobController.attributes.add('_camera', { type: 'entity' });
HeadBobController.attributes.add('_cameraHolder', { type: 'entity' });
// initialize code called once per entity
HeadBobController.prototype.initialize = function () {
this._time = 0;
this._toggleSpeed = 3;
this._startPos = new pc.Vec3(0, 0, 0);
this._startPos = this._camera.locaPosition;
};
// update code called every frame
HeadBobController.prototype.update = function (dt) {
this._time += dt;
if (!this._enable) return;
this.CheckMotion();
this.ResetPosition();
this._camera.LookAt(this.FocusTarget());
};
HeadBobController.prototype.CheckMotion = function () {
this.speed = new pc.Vec3(this._controller.velocity.x, 0, this._controller.velocity.z).magnitude;
if (this.speed < this._toggleSpeed) return;
if (!this._controller.isGrounded) return;
this.PlayMotion(this.FootStepMotion());
};
HeadBobController.prototype.FootStepMotion = function () {
var pos = new pc.Vec3(0, 0, 0);
pos.y += Math.Sin(this._time * this._frequency) * this._amplitude;
pos.x += Math.Cos(this._time * this._frequency / 2) * this._amplitude * 2;
return pos;
};
HeadBobController.prototype.ResetPosition = function () {
if (this._camera.locaPosition == this._startPos) return;
this._camera.locaPosition = Vec3.Lerp(this._camera.locaPosition, this._startPos, 1 * Time.dt);
};
HeadBobController.prototype.FocusTarget = function () {
var pos = new Vec3(transfrom.position.x, transform.position.y + _cameraHolder.locaPosition.y, transform.position.z);
pos += this._cameraHolder.forward * 15;
return pos;
};
There’s lots of issues like this throughout the code and stem from using Unity keywords/classes etc instead of PlayCanvas which I will leave for you to have a go at.