How to create movement speed limits when a character overlaps the surface of the water - unreal-engine4

How to adjust the speed of the character when crossing a cube with water?
And how to make it move more slowly on the surface of the water, while leaving the ability to move with Shift?
Speed up movement when pressing Shift
Water surface blueprint
Water surface blueprint components

Put an OnComponentBeginOverlap event on 'BP_WaterRegion' that checks what kind of actor it's just overlapped with. If it's the player character then you can set the MaxWalkSpeed variable on the character from there. The OnComponentEndOverlap event should be self-explanatory now.
If you select 'WaterMesh' you should find these events at the bottom of the Details panel.
(It's a while since I've worked in UE - you might need to connect the output from the cast node to the == node instead.)

Related

Precision in onCollisionStart in flame with flutter

I'm attempting to write a bouncing ball game using flame in flutter. To detect collisions the onCollision and onCollisionStart methods are provided. What I had hoped is that onCollisionStart would give a precise location when two objects first hit each other. However, instead it gives a list of positions indicating where the two objects overlap after the first game-tick when this happens (i.e. onCollisionStart is called at the same time as onCollision, but is not called a second time if the same two objects are still colliding on the next tick).
This is illustrated in the attached picture. The collision points are marked with red dots. If the ball were moving downwards, then the ball would have hit the top of the rectangle and so should bounce upwards. However, if the ball were moving horizontally, then its first point of contact would have been the top left corner of the box, and the ball would bounce upwards and to the left.
If I want to work out correct angle that the ball should fly off, then I would need to do some clever calculations to work out the point that the ball first started hitting the other object (those calculations would depend on the precise shape of the other object). Is there some way to work out the point at which the two objects first started colliding? Thanks
What you usually need for this is the normal of the collision, but unfortunately we don't have that for the collision detection system yet.
We do have it in the raytracing system though, so what you could do is send out a ray and see how it will bounce and then just bounce the ball in the same way.
If you don't want to use raytracing I suggest that you calculate the direction of the ball, which you might already have, but if you don't you can just store the last position and subtract it from the current position.
After that you need to find the normals of the edges where the intersection points are.
Let's say the ball direction vector is v, and the two normal vectors are n1 and n2.
Calculate the dot product (this is build in to the vector_math library) of the ball direction vector and each of the normal vectors:
dot1 = v.dot(n1)
dot2 = v.dot(n2)
Compare the results of the dot products:
If dot1 > 0, n1 is facing the ball.
If dot2 > 0, n2 is facing the ball.
After that you can use v.reflect(nx) to get the direction where your ball should be going (where nx is the normal facing the ball).
Hopefully we'll have this built-in to Flame soon!

Universal joint physics simulation in unity

I‘m trying to create real physics simulation of the universal joint in unity. In my case universal joint is in vertical position. The whole mechanism consists of four objects: white ceiling, green shaft, blue universal joint and red shaft. All parts have Rigidbody component, with specific mass (ceiling – 1kg, green shaft – 0,05 kg, blue universal joint - 0,01 kg, red shaft – 0,05 kg). The mass of center of the parts are represented in the pictures. All objects dimensions are NOT scaled and represent the real dimensions of an object in cm. The Box Colliders or Mesh Colliders were not added to the objects.
Green shaft center of mass point, Blue universal joint center of mass point, Red shaft center of mass point
The parameters for all objects are represented in the pictures: Green shaft parameters, Blue universal joint parameters and Red shaft parameters. To the ceiling object only Rigidbody component was added with an “Is Kinematic” option, without the “Use Gravity” option. One Fixed Joint and two Hinge Joints are used for objects. My aim is to create realistic physics behavior of this universal joint in simulation. For example, while playing the scene I want to be able to move the red shaft end, let go it and see how because of gravity and friction red shaft swings back to initial position. I want to do this with object components and project settings, not with a script if possible. If I succeed with universal joint physics I will later plan to add a point on the red shaft object end, which would have Constant Force component and would pull the red shaft end on demand with force to the certain direction. But the force which would pull the red shaft end would be based on real measurements.
The problem that I’m currently facing right now is with the object swinging back to initial position. If I leave Rigidbody and Hinge Joint parameters as it is represented in pictures and try to move red shaft end in scene play mode, the red shaft and blue joint swings back to initial position very very slowly. Like it would have no mass or would have very high friction. But if I turn on red shaft and blue Use Spring parameter in the Hinge Joint and set Spring value for example to 0.2 the shaft as expected goes back to initial position much faster. I also noticed that if I increase object Scale parameters (increase object size) for example from 1 to 100 it swings faster without the spring parameter turned on.
My first question which component or project parameters have the most impact on pulling object down in gravity direction?
Is where more friction friction parameters about which I should know while creating this universal joint model? Because so far I have changed only Drag, Angular Drag to 0, I have created a PhysicMaterial with almost no friction and added it to Default Material field in Project Settings->Physics. I have increased Default Solver Iterations and Default Solver Velocity Iterations almost to the maximum.
My third question would be, is it even possible to create a realistic universal joint shaft swinging without Spring option turned on in the Hinge Joint? Or should I better write the script that would define the red shaft swinging with gravity behavior?
First, Unity physics are not real physics, they are real-looking physics.
Second, Unity units are typically assumed to be in meters. When you set a position to <1, 0, 0> you should assume your object is at 1 meter in x.
Third, Unity physics uses colliders to determine volumes, for the purposes of moments of inertia.
So, when you say that
The mass of center of the parts are represented in the pictures. All objects dimensions are NOT scaled and represent the real dimensions of an object in cm. The Box Colliders or Mesh Colliders were not added to the objects.
It makes me think that (1) you are using the wrong scaling, like 1 = 1 centimeter instead of the assumed 1 = 1 meter, and (2) you're preventing Unity from being able to really run the physics calculations correctly because you're not providing volumetric information to the physics engine (which again is done via the collider volumes).
Also, your masses seem very, very, very small. You've got the universal joint at 10 grams, which is really not much at all.
You're seeing better results when you add a spring because the spring is adding a force, where your weights are small and your missing colliders are failing to do much with physics.
I would suggest adding mesh colliders and increasing the weights to get the behavior you want to see.

Unreal Engine 4 - Collider component sweep issue

I have a 2D environment where an object falls (a trap) and a box collider component is moved with a timeline to follow the movement with SetRelativeLocation.
Since the trap is near a wall if I leave the Sweep option unchecked in some cases the character will stuck mid air between the trap and the wall.
Sweep off
If I turn on Sweep this will not happen and the character is pushed to ground correctly but since the collider "slows down" for some frames it seems to be in the middle of the sprite.
Sweep on
Teleport option seems to have no effect on this behaviour.
Is there any way to have the sweep effect with the collider keeping his velocity and keeping the structure as it is with the collider moved by a timeline?
I feel like I'm missing something really simple but I'm losing my head on this.
Thanks!
Since it's 2D, have you tried blocking the input when your character collides? I would add a simple collider in front of your character and when it is overlapped, I'd limit the input in the direction your character goes, ie.:
Character collides right
MoveRight(float Scale): Clamp Scale to (-1.f, 0.f); AddMovementInput(...
Hope it helps!

Preventing an object from landing on its side; ensuring it always lands face-up or face-down

Currently I am doing a project of shell toss in Unity. The shell is made in Blender and imported in Unity. I attached a single box collider to the shell.
Now when I apply the random force to the shell to flip it, it sometimes rests in the vertical up position when either a result of heads (face-up) or tails (face-down) should have occurred.
I have tried changing the axis, increasing the gravity. But none of these solutions worked.
What I have done for now is when the shell is in a resting position after it lands, I rotate the shell 90 degrees - but this is not a great solution, since it is slow and takes time to check whether the shell has come to rest or not.
I am looking for the better idea so that the shell should only rests in a heads or tails state.
If you use a box collider for your shells, you are going to end up in this problematic situation where the flipped shell sometimes ends up sitting sideways, instead of face-up or face-down.
In this situation, I would recommend making use of Capsule Colliders, which are cylindrical colliders with a rounded top and bottom. If a capsule collider lands on either end, it'll fall over sideways:
However, one capsule collider isn't enough - otherwise, the shell will start rolling around after it falls over. I suggest 2 or 3, oriented in a cross such that their tips correspond to the sides of the shell:
Now, if the shell lands on any of its sides, it will topple over onto one of the faces of the cross - either face-up, or face-down. To add capsule colliders to your GameObjects, just click on Add Component, and select Physics > Capsule Collider. Then modify the properties to get them into the desired positions:
The Direction you choose should probably be X-Axis and Z-Axis. Once you have the correct orientation, change the Center value to move the colliders to the right positions to make a cross through the shell. Then, alter the Radius of the colliders to affect the thickness of the cross, and the Height so their tips line up with the edges of the shell.
Hope this helps! Let me know if you have any questions.

New to unity and particles & gravity

I am fairly new to Unity3D and thought I'd give it a go to make physics models.
Here is what I would like to do:
Particles, white small ones, that "stream" outwards from a point, 360 degrees(4/3*πr3) in a shape of a sphere, a growing sphere. The paritcle system should release new bursts of "expanding spheres" quite rapidly and sometimes not as rapid. If I add another primitive to the scene, the particles should bounce in the opposite direction when they hit the surface of that primitive and when they bounce the particle "loses energy" and starts to fade away eventually.
I found the "Particle System" but it does not seem to spawn a ready flow of particles to generate a "exploding effect" that I am after. I hope someone with more experience could help me out :)
You could use the options under the particle system called bounce. and set it from 0 to 1 depending on how much you wanted it to bounce off. everything inside of the collisions tab is what you want, and adding a set amount of burst, would give the impression of a wave, bursts are under emission they need an amount and delay from start of the system.
For the sphere shape you go under the shape category and change shape from cone to sphere.
hope this helps.