How to shoot ray in specific direction in UE4? - unreal-engine4

I'm trying to draw a debug line of certain length starting at my pawn position and moving in direction of mouse click, but I keep getting really weird results. Sometimes it works, but mostly does not. Seems like it depends on my character's location.
blueprint: https://imgur.com/e5MLOln
screenshots: https://imgur.com/n7KBHI8

Your problem is that your ray trace starts at a (correct) world location, but it ends at a relative location, since you're multiplying the X and Y coordinates of a unit vector by 500 - this will result in a value between (-500, -500, 0) and (500, 500, 0)
Instead, you want to add your multiplied unit vector to your line start location, which will give you a world location.

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!

Position x slowly increasing and rotation y slowly decreasing while going forward

Just starting out with unity, I simply just want to move the object forward in a straight-line. However, there are two things I'm noticing and I'm not sure why they're happening.
First, before starting the program, the Player's (Cube) transform position is set to (0, 1, 0). When I first simply run the program, the position changes to (-3.429751e-08, 1, -2.532359e-08) without me touching or doing anything further.
The second issue I'm having is, when pressing the forward key, "w" in this case, position x will slowly increase while rotation y slowly decreases. So the Player slowly drifts to the right while rotating slowly to the left.
All Slippery does is delete all friction from Ground. PlayerMat is just to change the color of the cube.
Basic code, Full Screen before start, Screen after start before and after moving are attached:
PlayerMovement Code
Full Screen BEFORE Starting Program
Screen AFTER Starting but didn't do anything else. Observe the random change of positions x and z
Screen AFTER Starting and moving forward a bit. Observe the random increase of position x and random decrease of rotation y
I tried resetting the position to (0, 1, 0) at the start using my Movement script as well as manually doing it in transform, but it stays (-3.429751e-08, 1, -2.532359e-08) either way. I also tried updating the rotation every frame to keep the rotation (0, 0, 0) using my Movement Script. However, the Player then drifts to the left.
Number representations in computers always have rounding errors because the space to represent numbers (e.g. 32 bit for float) is discrete. -3.429751e-08 is a very small value and can be considered zero. For the same reasons these little rounding errors stack up with repeated calculations. You are using the physics engine of Unity which will do a lot of calculations in the background pushing your cube around and thus changing the transform. The occuring rotational drift is a "natural" consequence of these calculations. That why you can tell the rigid body component to constraint such unwanted drifts. In your rigid body component, look under Contraints > Freeze Rotation and check the Y box. This should fix your movement.

What's the difference between ScreenToWorldPoint and ScreenPointToWorldPointInRectangle?

What's the difference between ScreenToWorldPoint and ScreenPointToWorldPointInRectangle? And when should we use which one?
Senario:
I'm using UI system creating my card game similar to Hearthstone. I want to transform my mouse drag positions to world position. RectTransformUtility.ScreenPointToWorldPointInRectangle(UIObjectBeingDragged.transform.parent as RectTransform, Input.mousePosition, Camera.main, out resultV3) works fine. But I also tried Camera.main.ScreenToWorldPoint(Input.mousePosition), and it give a different and "wrong" result.
ScreenToWorldPoint
Gives you a world position (the return value) that is along a ray shot through the near plane of the camera (the Camera whose method is being called) at some given point (the x and y components of the position parameter) and a given distance from that near plane (the z component of the position parameter).
You should use this when you:
have a specific distance from the near plane of the camera you are interested in and
don't need to know if it hit inside some rectangle or not
You could think of this as a shortcut for Ray.GetPoint that uses the x and y of position and various info of the Camera to make the Ray, and the z component of position is the distance parameter.
ScreenPointToWorldPointInRectangle
Also gives you a world position (worldPoint) along a ray shot through the near plane of a camera (cam) at a given point (screenPoint). Only this time instead of giving you the point a given distance along the ray, it gives you the intersection point between that ray and a given rectangle (rect) if it exists, and tells you if such an intersection exists or not (the return value).
You should use this when you:
have a specific rectangle you are interested in the intersection with a camera ray,
You don't know the distance between the camera or its near plane and the intersection point
Want to know if that rectangle is hit by the ray or not.
You could think of this as a shortcut for Plane.Raycast which uses cam and screenPoint to make the Ray, and rect to make the Plane, and also gives some more information of if it would intersect outside the boundaries of the rect.

Determine the direction an object is actually moving

In unity I am trying to compare the players actual direction with the direction they are facing and wish to move in but having major issues trying to find the actual movement direction.
I can determine the facing direction very easily with:
wishDir = transform.localEulerAngles;
But I cannot figure out how to get the objects movement direction so that I can compare. I have tried:
transform.InverseTransformDirection(rb.velocity);
I would expect this to be equal to 0,90,0 when I move to the right however it is equal to 0,0,0 (although jumps when there is acceleration).
How can I determine the direction an object is moving in?
I can determine the facing direction very easily with:
wishDir = transform.localEulerAngles;
This is already quite odd to me. localEulerAngles is a rotation in Euler space notation in degrees per axis x,y,z .. this is no "direction".
Usually if you want the direction you are looking in you would rather use transform.forward
wishDir = transform.forward;
And then
transform.InverseTransformDirection(rb.velocity);
should indeed return the direction in local space.
Note that the Debug.Log beautifies (rounds) this value to make it more human readable. If you want the exact values you could try and log e.g.
var relative = transform.InverseTransformDirection(rb.velocity);
Debug.Log(relative.ToString(F4));
which should print the values always with 4 digits after the decimal point.

transform.Translate not working properly in Unity

I am developing a game in which I want infinity road, So I created 40 instances of the road which I want to repeat again and again.
I am writing the following code to change the position of road that is not visible in the camera now and can be repeated
roadPaths[currentRoad].transform.Translate(Vector3(0,0,startPositionValue));
But I am getting the following log
In the above log you can see when the startPositionValue is 42, currentRoad position becomes 43. Same issue is with other values as well.
A help will be highly appreciated.
Translate is a relative move of the GameObject in three-dimensional space. It literally translates it from the current position by a relative value to a new position that is an offset from the original.
Explicitly setting the Position vector of the GameObject's Transform will set it to an absolute position within the world.
Finally I am able to solve the issue.
The following code work
roadPaths[currentRoad].transform.position=Vector3(0,0,startPositionValue);
I think this is very simple:
transform.Translate(X, Y, Z);
for exemple:
transform.Translate(5 * Time.deltaTime, 0, 0);
//GameObject moves smooth at the positive direction of the X position