x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Prevent actor bounce on collision

Hi all,

I'm new to UE4. Currently, I'm trying to make a simple breakout game. I created the paddle as an Actor in the game. The paddle is controlled by the left and right arrows. I'm doing most of the game logic and input handling in C++. The problem I currently have is when the paddle hits one of the side walls, the paddle seems to bounce back and continue moving in the direction it bounced even though I'm not holding down an arrow key.

The paddle has a cube component which simulates physics with a mass of 0. Simulation Generates Hit is checked since I want to be able to hit the ball and call custom code. The side walls also simulate physics and and have simulation generates hit checked.

The APaddle class has the following code:

 APaddle::APaddle()
     : m_paddleSpeed(500.0f)
 {
     PrimaryActorTick.bCanEverTick = true;
     m_velocity = FVector(0.0f, 0.0f, 0.0f);
 }
 
 void APaddle::BeginPlay()
 {
     Super::BeginPlay();
 
     InputComponent->BindAction("MoveLeft", IE_Pressed, this, &APaddle::LeftKeyPressed);
     InputComponent->BindAction("MoveLeft", IE_Released, this, &APaddle::DirectionKeyReleased);
     InputComponent->BindAction("MoveRight", IE_Pressed, this, &APaddle::RightKeyPressed);
     InputComponent->BindAction("MoveRight", IE_Released, this, &APaddle::DirectionKeyReleased);
 }
 
 void APaddle::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
 
     FVector NewLocation = GetActorLocation();
     NewLocation += m_velocity * DeltaTime;
     SetActorLocation(NewLocation);
 }
 
 void APaddle::LeftKeyPressed()
 {
     m_velocity.X = m_paddleSpeed;
 }
 
 void APaddle::RightKeyPressed()
 {
     m_velocity.X = -m_paddleSpeed;
 }
 
 void APaddle::DirectionKeyReleased()
 {
     m_velocity.X = 0.0f;
 }

When the paddle hits a wall and I release a key, the m_velocity is 0. I don't understand why the paddle continues to move. Any help is greatly appreciated. Thanks!

Product Version: UE 4.19
Tags:
more ▼

asked Jul 02 '18 at 04:25 PM in Using UE4

avatar image

Ben Katt
1 2 1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

The first thing I would suggest is, since it is receiving input, to make your Paddle class inherit from Pawn instead of Actor. Pawns are Actors that are meant to be "possessed" and receive input from controllers (or KBM).

To fix your problem with input, though, I would suggest making your MoveRight input action an axis where your inputs to move left have a scale of negative one. Using an axis mapping over an action mapping will allow you to directly set your "m_velocity" X value to the input value as the callback is called even when the axis value is 0.

more ▼

answered Jul 02 '18 at 07:47 PM

DarkwindRichard (suspended)
avatar image Ben Katt Jul 02 '18 at 08:16 PM

Thanks for the suggestions! I changed to use a Pawn and used an axis for input; it made the code simpler. I still have the same problem with collisions though. Any other ideas?

avatar image DarkwindRichard Jul 02 '18 at 08:38 PM

I've just created a small project in an attempt to repro this, and it seems like the bouncing issue may be stemming from having physics being simulated on your paddle.

avatar image Ben Katt Jul 02 '18 at 08:51 PM

Thanks again. I found that if I disable physics then the bouncing doesn't occur, but the paddle can pass through (and slightly move) the wall which is problematic. In short, I just want the wall to cause the paddle to stop moving. Any idea what would cause the paddle to pass through the wall?

avatar image DarkwindRichard Jul 02 '18 at 09:22 PM

Your paddle is moving through your wall because you're directly setting its location with SetActorLocation so it's not taking collisions into account. What you can do in your Tick method is use UWorld::LineTraceSingleByProfile to see if there would be a collision (i.e. if the X value of "MyHitResult.Location" is less than your paddle's location plus/minus half of your paddle's width plus your velocity's X value). If there was a collision that close, then you would only move up to the wall. If not, then you would be safe to use your full velocity movement.

EDIT: I would also only suggest performing the line trace if your velocity's X value is not zero. It can be an expensive operation, so you wouldn't want to use it even in frames when you're not moving.

avatar image Ben Katt Jul 05 '18 at 08:30 PM

Thanks a ton! I got it to work. I seriously appreciate the time you took to help me.

avatar image DarkwindRichard Jul 05 '18 at 08:49 PM

Not a problem! I'm glad I could help you solve your problem and learn a bit more about the engine :)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question