How do you change the capsule component to a sphere component?

I want to change the root capsule component of my class derived from ACharacter to a sphere component. Is this possible, and if so, how do I programmatically change it?

Try to remove the component with functions and then add new component in place of root component

… With what functions?

use this Method

USceneComponent::DestroyComponent()
and
USceneComponent::AttachTo


UFUNCTION(BlueprintCallable, Category="Utilities|Transformation", Meta=(AttachType="KeepRelativeOffset"))
void AttachTo
(
    class USceneComponent * InParent,
    FName InSocketName,
    EAttachLocation::Type AttachType
)

There are MANY different issues with that. For one, destroying the capsule component, which is the root component, does not actually stop anything from automatically attaching to it. If I now attach everything to the sphere component, the sphere is still parented to root, which is a now empty part. And the character doesn’t get effected by gravity, and is not able to move or jump. I need actual code to properly change the cylinder component to a sphere component and still have the character controller logic work…

Is there seriously no one able to answer this question? How is changing the component not an easy thing to do?

Hi DoctorWhy,

You don’t want to remove the capsule component from your character, because, as you have noticed, all of the movement functionality is tied into the capsule component. Instead, you can decrease the height of the capsule component to turn it into a sphere.

In your constructor:

// Set size for collision capsule
CapsuleComponent->InitCapsuleSize(42.f, 42.0f);

This sets the half-height of your capsule the same as the radius of the capsule, essentially giving you a sphere.

The other question is: Why do you need a sphere component? If you want to create something like a rolling ball, you may not need to use a Character class at all. In the rolling ball template projects, the “character” is not derived from the Character class, and does not have either a capsule or sphere component. Instead, it is a physics object where movement is obtained by applying forces to the ball in different directions. If instead you want a sphere component for a really short character, the code above is probably what you want.

If you want to be able to change the capsule on the fly, you can also use:

CapsuleComponent->SetCapsuleHalfHeight(float HalfHeight, bool UpdateOverlaps);
CapsuleComponent->SetCapsuleRadius(float Radius, bool UpdateOverlaps);
CapsuleComponent->SetCapsuleSize(float Radius, float HalfHeight, bool UpdateOverlaps);

As long as your capsule’s half-height and radius are the same, you will have a sphere.

Ya, I actually figured out the height thing a few days ago. It will work, I guess.

The reason I wanted the ACharacter class is for the movement logic. I want the ball to move right when the button is pressed and stop right when the button is up, as well as not be affected by hills or stairs. Another thing is the jumping. The logic they have in the example is “if it touches ANYTHING no matter what, the ball can jump again”, which doesn’t make any sense. I don’t want to write my own code (until after the prototype) to jump or move properly.

Either way, I figured out everything I need and this is one of a few steps it took to get it the way I want it.