The turret of tank can not be turn towards the player pawn? where is the problem? - unreal-engine4

I have a simple game done using unreal engine 4,
The turret of the tank should turn toward the player pawn.
I made the blueprints code to build that, but the turret rotates quickly and incorrectly.
Video about problem
I tried to solve the problem many times without any results !!
Hope to help me.

There are probably a few ways you can do this, but you can simplify this quite a bit. I also suspect you can do all of it in the animation blueprint.
Simplify the first part (XDoMa.jpg) using the Find Look At Rotation node.
Break the output of this and create another rotator with the yaw from that node and the pitch and roll from the turret's world rotation (you're currently setting pitch and roll to zero in cDJ08.jpg, which you don't want to do in case the tank is on a slope).
To rotate the turret to point at the target, use the RInterp To node and connect its output rotator to a Set World Rotation node. The nice thing about the 'RInterp To' node is that you can set the speed. For a bit more realism you could use some simple maths to accelerate and decelerate the turret's rotation.
Another thing you'll want is a bool variable in the tank actor BP. Then you can use a branch in the animation BP to trigger the stuff above when you set the bool to true.
The key parts to this are the Find Look At Rotation and RInterp To nodes, and to do all of it in the animation BP.

Related

Unity Player passsing through objects

player and object both have colliders and rigidbodies, object has position and rotation locked, player has only rotation locked. When the player goes to the blocks, the player goes through the blocks, although they do give a bit of resistance. To move the player im setting the rigidbody's velocity, and doing that in FixedUpdate.
i have no idea why this is happening, any ideas?
main part of the code is:
rigidBody.velocity = new Vector3(direction.x, rigidBody.velocity.y + (-Gravity * Time.deltaTime), direction.z);
(direction is determined by the WASD keys, and i'm using my own gravity)
First of all, you do not need to multiply the velocity by time.DeltaTime, because you are moving your object in the FixedUpdate() method; Which uses fixed time intervals since the physics engine does not run in sync with the regular game engine. Also, both objects do not need rigidbodies in order to collide with one another. I suggest looking at your collision matrix in settings and verifying that everything you need collision for is checked correctly. As others have said as well, check your kinematics on the rigidbody.
A last suggestion for working with your own gravity. Do not change the actual gravity value of the game engine. It is typically recommended that you use a multiplier variable and apply it to the constant gravity already set by the physics engine. If you are completely editing the gravity, than maybe consider using a character controller instead.
I guess it has something to do with what the documentation says "In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour".
Try to use AddForce() or similar functions to alter the properties of the rigid body. Colliders etc will then work as expected.

Unity: Fish with rigid body leaving water

I am making a simulation of some fish in a lake.
I am using rigid body as otherwise they move through the sides. But when they hit into the sides they rotate and then the script I have to set their rotation to 0,0,0 when they leave fails.
Your problem may be occurred by the fact that you are using unity's physics to move the fish but trying to set rotation directly. In this case movement and rotation of the fish are driven by Physics and forces, applied to the object. But you still can affect rotation and position if you reset all forces. Try looking here to learn how to remove forces.
Also when dealing with Physics in Unity, don't mess Update() and FixedUpdate() methods, because it may lead to an incorrect result.
When you say "hit into the sides" do you mean the sides of the simulated lake?
If so, one option might be to set the objects that denote the sides as "kinematic." Then you can detect when a GameObject (such as a fish) collides with it but it won't cause any physics reaction.
(ref: RigidBody)

Unity: Ball falls through object that is rotating around another object

Imagine you have a ball falling due to gravity. When it encounters a rotating "cube" object, then you would expect it to bounce off of the object. However, in my case if the cube is rotating fast, the ball goes through it, but if the cube is rotating slowly, the ball hits it and bounces away as expected.
I am using RotateAround() inside the Update() method to achieve the "cube" object's rotation. I tried setting the ball's collision detection to Discrete, Continuous, and Continuous Dynamic with no luck.
My goal is to make the ball bounce away no matter how fast the "cube" object is rotating around another object.
Is there something else I should look into?
Thanks!
You can try lowering the Fixed Timestep value under Edit > Project Settings > Time.
Be aware that this will affect the performance of the game as you're calculating physics more often.
Documentation: http://docs.unity3d.com/Manual/class-TimeManager.html
Also, I assume that you have box and sphere colliders as opposed to mesh colliders? The former are more efficient at detecting collisions.

Unity physics about collision and energy

I have been working on a Unity ping pong game using the Leap Motion. I use rigidbody.MovePosition() to move the paddle. However, when I hit the ball (which uses gravity), the paddle launches it too far. Even when I change the masses of both, it doesn't do anything.
What variable should I change to reduce this energy going into the ball?
From the following link.
"MovePosition will put your object at the target location, no matter what. It may push aside other objects in a realistic way, or may blast them out of the way, or may just pass right through them. But it will gladly move you through a solid wall or a mountain.
If you're using MovePosition on a rigidbody to add from where you currently are, it looks like AddForce. With AddForce, the physics step does all the work: applies your velocity, sees the collision and handles the bounce. With MovePosition, the physics step sees you're mysteriously overlapping a solid object. If it isn't too much, it will bounce you apart."
You won't need to use MovePosition. Instead, you can figure out the direction of the shot (based on the position of the ball relative to the paddle). Then you can add an impulse force in that direction to the ball.
Pseudo-code (from the following link):
Vector3 shootDir = ballPosition - paddlePosition; // Calculate direction of the shot
shootDir.Normalize(); // Normalize the shot vector
ball.AddForce(shootDir * speed, ForceMode.Impulse); //Add impulse force in correct direction.
Credit due to Owen Reynolds and Tim Michels.

Slope limit on player movement

I use Unity3D and I have a player with a rigidbody. I add force tto the body for moving the player. My player walks over a terrain but is able to walk up mountains that are to steep to climb. I want to limit the player so it cannot walk up a slope that is to steep.
I know there is a CharacterController component that has this functionality, but I have to use the rigidbody, so I want the same but on my rigidbody.
I can get the normal of the triangle I am standing on, and calculate its angle, but I cannot seem to make the player stop moving up the slope. Only make the player stop moving (which makes the player unmovable once it hits a angled slope)
Any ideas how to solve this problem?
It's difficult to answer without more details on how you're using the physics engine. How/Are you using friction? What angle are you applying the force? Is it always horizontal or at the angle of the floor? Does the player have a mass?
Anyway I can think of a few ways to solve this
Go the pure physics route. Using player mass, friction, force angle, gravity, etc. Get the physics to handle these situation for you. This may take a fair amount of time and programming.
Keep the rigid body but fake the forces. Scale the force you are applying to the body of the player with the angle of the triangle the player is on. You can either use trigonometry to work out what you should apply or your own mapping. By your own mapping I mean set an angle where 0 force is applied (say 45 degree) and do a linear(or non linear) scale on the force applied so on flat ground force is 1 and at 45 force is 0.
Don't use rigid bodies. There is a reason most games don't use rigid bodies to control characters. It's hard and complicated and most of the time not worth the time it would take. Of course I don't know the details of your project so if this isn't an option, fine.
Hope that gives you some things to think about.