Hello !
I’m working on a game but I have a problem : when I rotate an object the Vector3 of his position follow its rotation, so it’s usefull to rotate player and keep its “forward” direction.
But I want to keep my world axis when I change the rotation of this object. There is a function for it ?
For example :
My camera doesn’t move
I have a chair, with the z axis like this and the x axis like this . If I do a translation with 90° (with this.gameobject.rotate()) now the z axis is like this and my x like this .
But I want to keep the initial transformation axis, how could I do that ?
Original Post on Discord
by user 224464722878005248
marwie1
(marwi)
December 16, 2022, 12:00am
2
There is a theejs method for it yes
marwie1
(marwi)
December 16, 2022, 12:00am
3
I tried it but, for example this cube :
by user 224464722878005248
by user 224464722878005248
With my arrows, I can rotate on the Y axis
by user 224464722878005248
But with rotateY or this.gameObject.rotation.y or this.gameObject.rotateOnWorldAxis( new Vector3(0,1,0), -1 *this.context.time.deltaTime * this.speed )
by user 224464722878005248
I get the same result, which is :
by user 224464722878005248
by user 224464722878005248
And I would like this :
by user 224464722878005248
by user 224464722878005248
Is there a way to always translate objects along the same static axis (like the world’s) no matter how they rotate? Without using a parent?
by user 224464722878005248
Like this :
transform.Translate(new Vector3 (0, -fallingSpeed, 0) * Time.deltaTime ,Space.World);
In unity ?
by user 224464722878005248
marwie1
(marwi)
December 16, 2022, 12:00am
15
That’s exactly what rotateOnWorldAxis does
marwie1
(marwi)
December 16, 2022, 12:00am
16
import { Behaviour } from "@needle-tools/engine";
import { Vector3 } from "three";
export class Rotate extends Behaviour
{
update(): void {
this.gameObject.rotateOnWorldAxis(new Vector3(0, 1, 0), 0.01);
}
}
So it doesn’t affect the x y or z translation ? It keeps the same direction ?
by user 224464722878005248
Because it doesn’t work like this for me :
update() {
// On avance
if (this.context.input.isKeyPressed("q")) {
this.gameObject.translateX(-1 *this.context.time.deltaTime * this.speed)
}
// On recule
if (this.context.input.isKeyPressed("d")) {
this.gameObject.translateX(this.context.time.deltaTime * this.speed)
}
// On tourne Ă gauche
if (this.context.input.isKeyPressed("z")) {
this.gameObject.translateZ(this.context.time.deltaTime * this.speed)
}
// On tourne Ă droite
if (this.context.input.isKeyPressed("s")) {
this.gameObject.translateZ(-1 *this.context.time.deltaTime * this.speed)
}
if (this.context.input.isKeyPressed("ArrowRight")) {
this.gameObject.rotateOnWorldAxis( new Vector3(0,1,0), 1 *this.context.time.deltaTime * this.speed )
}
if (this.context.input.isKeyPressed("ArrowLeft")) {
this.gameObject.rotateOnWorldAxis( new Vector3(0,1,0), -1 *this.context.time.deltaTime * this.speed )
}
}
by user 224464722878005248