Hello guys
I got this problem where I am rolling my ball in one direction at constant speed and forever.
But there is a special platform where ball needs to stop at when X and Z are the same, meaning when ball comes in the center of the platform, it should stop.
That means that ball will be at (0, 1,5f, 0) and platform (0, 0, 0)
This script is attached to a ball.
My code:
RaycastHit hit_2;
Ray ray_2 = new Ray(transform.position, Vector3.down);
if(nazovSceny == "scene"){
if(Physics.Raycast(ray_2, out hit_2)){
//Debug.DrawRay(ray_2.origin, ray_2.direction, Color.green);
if(hit_2.collider.tag == "sipky"){
//vHit = new Vector3(hit_2.transform.position.x, transform.position.y, hit_2.transform.position.z);
if(transform.position == vHit){
if(hit_2.transform.rotation.eulerAngles.y == 0){
x = 0.0f;
z = 0.0f;
}}}}}
This:
vHit = new Vector3(hit_2.transform.position.x, transform.position.y, hit_2.transform.position.z);
if(transform.position == vHit)
is literally ignored, so what am I supposed to do? Where am i wrong?
Without that part it works but just as it immediately touches that platform meaning the edge … I NEED IT TO STOP AT THE CENTER OF THE PLATFORM
So anyone ?
I read somewhere that comparing current and target positions as Vectors is somehow… bad?.. for moving object.
Can’t tell why but is that the case?
Basically, you’re almost never going to find a case where two Vector3’s are exactly equal unless you have explicitly set the values to be so. One may be (3.00001, 2.9997, 1.00002), and it won’t match (3, 3, 1) - and due to a number of things that can cause miniscule variations (raycasts, time.deltaTime, and literally any math operation at all thanks to floating point imprecision), Vector3’s will rarely be exact numbers.
You also shouldn’t use eulerAngles the way you’re using them - literally the only thing Euler angles are good for is human-friendly typing in of angles, but in any other scenario they don’t give you predic. Rather than checking to see if eulerAngles.y is a particular value, use another technique depending on what you’re trying to accomplish - probably check to see if transform.forward.y is 0. But again, you’ll want to check a small range of values, because of floating point imprecision - you might get a result of 0.0000001 or something.
You cannot compare exact vector values since framerate factor and velocity of the object need to be considered. Comparison is made every frame and at higher velocity there can be quite a big difference between position from previous frame and position at the current frame. Also consider different framerate on different machines. On machines with slow fps small deadzone might not work as there will be bigger gap between the positions. To avoid this problem use time.deltaTime.
Oh i see now… but then if i check for a range… will the object also stop at that range and not exactly at the desired location?
If yes, then can i set the object to desired position instantly when it will be in that range? … since range between desired position and range will be very small…
And also which method mentioned above should I use?
Between my code sample and kubajs’s code, either one - they do fundamentally the same thing. Mine is slightly easier to read/understand at a glance (which is why I picked it), kubajs’s will execute faster (because sqrMagnitude is like Distance, but doesn’t perform the square root part of the distance calculation - it’s fine if you need to compare dist < x like you do here, but is not suitable for operations that do math on the actual distance.) The difference between the speeds is tiny, though, unless you’re doing hundreds or thousands of those checks per frame.
Alright, also what should i set acceptableDistance to ? .001 or .0000001?
I can’t debug this values for some reason even when using float it only shows number with maximum of 1 decimal place when debugging transform.position while object is moving.
This is actually too frustrating for me… So lets break it down it to simplest form:
I have moving ball at constant speed using velocity, I want the ball to change direction meaning applying velocity value to Z instead of X and vice versa BUT only when it hits exact middle of the platform it walks upon and it continues in different direction until it hits another platform and its exact center … those platforms are just redirecting ball by changing velocity value between X and Z… I want it to work perfectly but i just can’t do it…
Setting approx range between current and desired(center of platform) positionwork just first time it hits the platform … but not another time because it hits range and not exactly the desired vector so it means it works first time, changes direction and rolls to another platform however it wont work another time because since it changed direction in the range, it continues with something like 1.01154561 or something like that… you get the point
I seriously need some help…
At this point I will accept even harder solution because I am running out of time and I am getting frustrated, tired and I feel like giving up.
I have an idea (only read your most recent post), but hopefully this is simple.
When you get your first hit, (and subsequent ones …) start sending the ball ‘towards’ the other contact point explicitly?
It sounds like you’re looking for a back and forth (or a few spots?).
Well, something like that but the ball always should be centered in order to change direction.
If I use approx range because of vector precision problems then i want to set ball position to exact center of platform when it gets to that approx range - player wont see this teleportation since difference between approx range and the exact center is very small.
I thought it would work, I thought I finally get it when suddenly this - transform.position = “desired position” gets the ball stuck in that position because it runs per frame constantly rather than just once…
What’s the solution? I am surprised such a small task is so hard to do…
Without seeing the code for setting it (and what check is done before the setting), it’s hard to say /correct.
However, going back to something I was trying to ask earlier: Does the ball go back and forth in a straight line always between 2 spots and you want it to turn around when it’s at the center?
If that’s the case, as I was trying to say before, simply send the ball explicitly towards that spot, & when it gets there, send it explicitly back. “MoveTowards” for example will always get you to where you want to go. Sure you have to check when you get there, and switch to the other spot, but it would solve your issues about miscalculations.
If my suggestion is off, because I didn’t quite understand your situation, what did I miss?
Yep that’s the case pretty much but I also need to keep physics working so I use direct values in velocity with Y modified so that it falls realistically when needed however movetowards ignores physics or physics happens in a weird way
Okay, that makes sense. Here is a good link that I believe you’ll find helpful maybe. Sort of combing the ideas of move towards but actually using physics properly…
I’m kinda at a loss, either that link wasn’t helpful or you weren’t sure how it could be helpful.
Save the 2 positions’ centers… Set the ball to go towards one with force, or velocity or whatever… maybe make a small collider in the area there, and when it hits that/trigger… send it towards location 2 using the same method …