# Where can I find WorldToComponent ?

I’m looking to transform the world z up vectors to my characters local capsule component Up Vector. I was told there is a method in the capsule component - but I only find Component to World.

Thanks!

I’ve looked all over the UE4’s stuff and I can’t find a world to component method. So I’m trying to assemble one and I’m curious about the functionality of UnRotateVector.

FVector UCharacterMovementComponent::hkvWorldToComponent(FVector VectorToRotate)
{
FRotator compToWorldTrans = CharacterOwner->CapsuleComponent->GetComponentToWorld().Rotator();
FVector rotatedVector = compToWorldTrans.UnrotateVector(VectorToRotate);
return rotatedVector;
}

The above code seems to produce the character spiraling towards the center (0,0,0). I was tested it on the DoJump, by Calling hkvWorldToComponent(0,0,GetGravityZ()).Z I was trying to convert the GetGravity.Z to Component space of the character capsule.

It’s simply the inverse of ComponentToWorld, so to transform a world direction vector into a local vector in a component’s space:

``````FTransform T = TheComponent->GetComponentTransform();
FVector LocalVector = T.InverseTransformVectorNoScale(WorldVector);``````

Okay so I’m having difficulty getting this to work. I think I’m missing something important. Here’s the code: (Also I don’t know how to paste code to make it look nice so sorry)

``````//World to Component Method
FVector UCharacterMovementComponent::hkvWorldToComponent(FVector VectorToRotate)
{
FTransform compToWorldTrans = CharacterOwner->CapsuleComponent->ComponentToWorld;
FVector rotatedVector = compToWorldTrans.InverseTransformVectorNoScale(VectorToRotate);
return rotatedVector;
}

/*Do Jump, Here I'm not using the method(Though I would like to) this works in getting the jump to be along the character capsule's upvector. */

Velocity += CharacterOwner->GetActorUpVector() * JumpZVelocity;

/*PhysFalling section,  changing this line to use the character capsule's up vector will work but I think it would be best to just rotate the vector so I can always use the Z is up logic */

Velocity = NewFallVelocity(Velocity, hkvWorldToComponent(FVector(0.f, 0.f, GetGravityZ())), timeTick);
``````

When I runt game, the character will jump up along capsules UpVector so that approach works, but once when the game starts the character falls from a height (so I can check that he’s falling along his UpVector) Using the inverse rotated Vector the character falls along the World “Z”. This is what I’m trying to avoid.

I’m still having trouble getting the correct vector. When I use CharacterOwner->GetActorUpVector() I get the correct vector that I’m looking for. What I’m trying to do is rotate the GetGravity to this so that I can still use the up logic throughout the character Movement Component. I was told that I could create a WorldToComponent function to rotate the vector to my characters so I could still reference the Z Axis when evaluating.

Currently the way I’m trying to rotate the FVector( 0,0,GetGravityZ) is producing the vector along which the character falls to be a bit more random.