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"

Limit the radius of movement of the player

Hi. I am trying to implement a restriction on movement by a player in a certain radius. How to prevent the player to go beyond the radius of the object? At the same time without restricting its movement within the region.

 if (isLocomotingRight) {
     // Player current position
     FVector CurrentPosition(this->RootComponent->GetComponentLocation());
     // The coordinates of the object around which we will walk
     FVector AnchorPosition(2091.0f, -11052.0f, 0.0f);
     // Calculate the distance to the object
     FVector vector = AnchorPosition - CurrentPosition;
     float AnchorDistance = vector.Size();
     // Radius of limitation
     float maxDistance = 220.0f;
 
     /*Here I need some code to impose a value on offsetDir*/
     
     // Next comes the calculation of the displacement
     FVector offsetDir;
     offsetDir = locomotionDirectionRightX * locomotionRightXMultiplier + locomotionDirectionRightY * locomotionRightYMultiplier;
     offsetDir.Z = 0;
     if (isLocomotionSpeedConstant) offsetDir.Normalize();
     AddMovementInput(offsetDir);
 }

Is there an elegant way to do this?

Product Version: UE 4.19
Tags:
more ▼

asked Apr 17 '19 at 09:18 AM in C++ Programming

avatar image

KuzbassCOT
3 1 2

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

2 answers: sort voted first

i would do something like that, but that could put some jitter back and forth. you could play with the dot product of offsetDir, -vector and if > 0 set offsetDir to 0 ?

 FVector offsetDir;
      offsetDir = locomotionDirectionRightX * locomotionRightXMultiplier + locomotionDirectionRightY * locomotionRightYMultiplier;
 
 if(AnchorDistance>maxDistance)
 {
 offsetDir+= vector.GetSafeNormal() * 2*offsetDir.Size(); // we go 2 time faster than current input back to start point ;
 }
 offsetDir.Z = 0;
more ▼

answered Apr 17 '19 at 04:12 PM

avatar image

Firefly74
1.5k 14 18 33

avatar image KuzbassCOT Apr 18 '19 at 03:26 AM

Yes, initially I also did a check, but with an indication of each axis separately. offsetDir.X Y Z And it was awful) Your option is more elegant. Jitter back is not critical. Thanks a lot for the function.

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

You don't need to code it, you can use mesh as a walks with a material with 0 opacity and collision

more ▼

answered Apr 17 '19 at 04:17 PM

avatar image

Blackforcedragon
0 3 8 11

avatar image KuzbassCOT Apr 18 '19 at 03:28 AM

Thank you, but I wanted to resolve this without collisions.

(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