Making a Growth and Shrink Function in Blueprints

So I am trying to make a sphere that when you press the Q and E the sphere would grow and shrink respectively. I thought I had figured it out. I was mistaken.

How would I set something like that up?
Please and Thank you :slight_smile:

1 Like

To create a sphere that can grow and shrink based on user input, you can use Unity and the following steps:

Create a new Unity project and import a sphere mesh or create one using the GameObject > 3D Object > Sphere menu option.

Create a new C# script by right-clicking in the Project window and selecting Create > C# Script. Name the script “SphereScaler”.

Double-click the script to open it in your code editor of choice.

Add the following code to the SphereScaler script:

using UnityEngine;

public class SphereScaler : MonoBehaviour
{
public float scaleFactor = 0.1f;

void Update()
{
    if (Input.GetKey(KeyCode.Q))
    {
        transform.localScale += Vector3.one * scaleFactor;
    }
    else if (Input.GetKey(KeyCode.E))
    {
        transform.localScale -= Vector3.one * scaleFactor;
    }
}

}

Attach the SphereScaler script to your sphere GameObject by dragging the script from the Project window onto the sphere in the Scene view.

Play the game and press the Q and E keys to grow and shrink the sphere, respectively. You can adjust the scaleFactor variable to control how much the sphere grows or shrinks with each key press.

This code listens for the Q and E keys using the Input.GetKey() method. When the Q key is pressed, the sphere’s scale is increased by the scaleFactor variable multiplied by Vector3.one, which represents a vector with values of 1 for each axis (x, y, and z). Similarly, when the E key is pressed, the sphere’s scale is decreased by the same amount.

Hope this is the solution for you!

Also I’m sorry that the code formats weirdly, don’t know why it does that.

This is interesting. Thank you :slight_smile:

1 Like

Oh shoot, this is for unreal courses isn’t it, I gave you something based on unity, dang how’d I get that confused, well hopefully you can maybe still take the concept of this.

I thought it was a touch odd, but it was interesting and I appreciated your willingness to help.

I ask questions on reddit sometimes and I get down voted for it.

Oh I see, well c++ and c# are kinda similar so hopefully the concept is there, however I want to help you out so here is what a c++ concept could look like:

Create a new C++ class that inherits from the AActor class.

In the header file of your new class, declare a USphereComponent member variable to hold the sphere mesh.

#include “CoreMinimal.h”
#include “GameFramework/Actor.h”
#include “Components/SphereComponent.h”
#include “MySphereActor.generated.h”

UCLASS()
class MYPROJECT_API AMySphereActor : public AActor
{
GENERATED_BODY()

public:
AMySphereActor();

virtual void Tick(float DeltaTime) override;

protected:
virtual void BeginPlay() override;

private:
USphereComponent* SphereComponent;
};

In the implementation file of your new class, define the constructor to initialize the SphereComponent member variable with a new USphereComponent and set it as the root component.

AMySphereActor::AMySphereActor()
{
SphereComponent = CreateDefaultSubobject(TEXT(“SphereComponent”));
RootComponent = SphereComponent;
}

Override the BeginPlay() function to set the initial size of the sphere and bind the Q and E keys to functions that will increase and decrease the size of the sphere.

void AMySphereActor::BeginPlay()
{
Super::BeginPlay();

// Set initial size of sphere
SphereComponent->SetSphereRadius(50.f);

// Bind Q and E keys to functions
InputComponent->BindAction("Grow", IE_Pressed, this, &AMySphereActor::Grow);
InputComponent->BindAction("Shrink", IE_Pressed, this, &AMySphereActor::Shrink);

}

void AMySphereActor::Grow()
{
SphereComponent->SetSphereRadius(SphereComponent->GetScaledSphereRadius() + 10.f);
}

void AMySphereActor::Shrink()
{
SphereComponent->SetSphereRadius(SphereComponent->GetScaledSphereRadius() - 10.f);
}

Override the Tick() function to update the size of the sphere every frame.

void AMySphereActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

// Update size of sphere
SphereComponent->SetWorldScale3D(FVector(1.f));

}

This is just an example, you would replace values to fit your needs, I’m not very good at c++, but hopefully this can help!

Thank you. I will definitely store this for later. :slight_smile:

1 Like

Anytime!

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms