Hi guys,
i’m learning javascript altogheter with unity engine and there is something i can’t understand in the FPSwalker.js example script:
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
The thing i can’t understand is the use of the keyword “new”. I know it is used to create a new instance of an object or class, but what i’m asking is: why do we need it? Couldn’t we just work with Vector3 ? In the example it would look something like:
moveDirection = Vector3(Input.GetAxis (“Horizontal”), 0, Input.GetAxis(“Vertical”));
And it would still work, because i tried it.
So what’s the use of “new” before Vector3 ?
Is it to prevent dangerous modification to Vector3 structure ?
But how is it gonna happen? If we are gonna work on moveDirection then moveDirection isn’t just a copy (a new one already because WE defined it as a new variable which didn’t exist before) of Vector3 structure ? So how is it gonna damage Vector3 ?
Please help me unfold this mistery ![]()
Enjoy your work.
A