- Home /
The question is answered, right answer was accepted
WorldSpace movement?
Hi, I'm trying to convert a vector for local space movement to a World Space vector for movement. Let's say I jump and I will move in the direction I jumped, except the problem is it is local space and relative to camera rotation and you will move relative to the camera, which is both right and wrong.
I'm trying to make it so you move in the direction you are facing, but when you jump in that direction, you'll turn around and you will still be going in the direction you were originally looking instead of always moving relative to the camera.
So I believe the solution to this is to have a world space vector and a local space vector, local space for inputs (So X is sidestep and Z is forward/backpedal, Y being vertical gravity and such) and the world space vector which takes the local space information, converts it to world space, and then treats them as accumulative values to simulate momentum with the CharacterController.Move method.
I've tried transform.TransformPoint, but my character floats towards the ceiling and then falls back down for a bounce. There must be some way to do this.. Any ideas?
Answer by Baste · May 14, 2015 at 08:20 AM
The world space forward vector is transform.forward. You also have transform.up, transform.left, transform.right, etc.
So when you jump, you store the current transform.forward (or the current movement input) as the jump direction vector, and use that for motion while in the air, never changing it before you land.
Yes but what about other things such as sliding in puddles and getting knocked back by explosions? Could this all be handled by a secondary vector that is for world space? $$anonymous$$y input vector is the character's rotation multiplied by the same vector, does that count as local space? I think it does, please correct me if I'm wrong.
The same technique can be used for everything that's supposed to lock your movement in a direction. Assu$$anonymous$$g that you've got some method $$anonymous$$ove that moves your character, then it'd look something like this:
bool directionLocked = false;
Vector3 lockedDirection;
//or in FixedUpdate, depending on how you move.
void Update() {
if(directionLocked) {
$$anonymous$$ove(lockedDirection);
}
else {
$$anonymous$$ove(transform.rotation * inputVector);
}
}
And then whenever you jump, start sliding, or start getting knocked back, you call a method somewhat like this:
void Lock$$anonymous$$ovementInDirection(Vector3 dir) {
directionLocked = true;
lockedDirection = dir;
}
So jumping would look like this:
void Jump() {
Lock$$anonymous$$ovementInDirection(transform.forward);
//Handle other jumping stuff, like a coroutine that moves the character on the y-axis or whatever
}
and so on. Whenever you should stop having your direction locked - when you detect that you land, or when the knockback is over - just set directionLocked to false again.
Hmmm I'll test this out. From the looks of this, this will disable your movement until you either land or slide out of a water puddle. Isn't there something where I can just move in the locked direction and simply add to it's values with an input vector? I want to give players some air control. $$anonymous$$inda like the way some old PC games have player momentum, like running and jumping in Half Life.
In that case, whenever your movement is locked, you lerp from the old movement to the new movement rather than setting it directly.
Yep I remember trying something like that before, but not with a movement lock boolean or while jumping. It works btw, it works great.
Follow this Question
Related Questions
rigidbody.MoveRotation neither world space or local? How does it work? 1 Answer
Box Collider Vertexes in World Space 3 Answers
Raycasting, How to draw a line to the position of the rays end when nothing is hit 1 Answer
How do I get the world space up direction for RotateAround? 0 Answers
vertices to world position 1 Answer