#Frame rate dependant stuff
1 messages · Page 1 of 1 (latest)
void Update()
{
Vector3 forward = rb.rotation * Vector3.forward;
Vector3 right = rb.rotation * Vector3.right;
Vector3 up = rb.rotation * Vector3.up;
Debug.DrawRay(rb.position, forward, Color.blue);
Debug.DrawRay(rb.position, up, Color.green);
Debug.DrawRay(rb.position, right, Color.red);
}
should give you the correct lines that unity shows you
only wrote it in update so you can see the lines appear, but should be in fixedupdate still
And no you dont need to convert rb.rotation. Unity has a quaterion * Vector3 declared, meaning it is a valid way of rotating a vector. We are rotating the world direction of "Vector3.up" to a local direction based on the rb rotation
these directions also probably arent the main source of the issue. I want to believe its the groundCheck.position but both are probably contributing to it.
so what I would do is just simply have a vector3 for the ground check offset. So then you do
groundCheckPosition = rb.position + offset
and suddenly its now in line with the rb position. Right now, especially with interpolate on, your objects are moving every frame and thus the child objects are too
Will try what you're saying, though are the rays supposed to show like this?
how is your object rotating? yea these lines are wrong
its weird that the lines are so long too, and showing after a delay as well
I believe it's rotating both in the transform and probably the rigidbody?
I think that's because I let them be infinitely long, and that probably slowed it down?
its only 1 unit long, because Vector3.forward and the rest are as well. its just simply rotated
theres a good frame too where lines are a normal length, but suddenly they expand out. Can you show anymore code like whats actually rotating this object?
Like whats keeping the rigidbody stuck to the wall
I think the value that is 1 is being directly multiplied by the rb.rotation
it seems to be lines up
Oh is it just the addForce doing that, and the rb is rotating via physics?
Yea, addForce is supposed to add a force downwards to the player, negating gravity and allowing them to stick to the circle slope
😅 alright this is suddenly confusing, though im assuming its probably a difference in 2D thats causing this because i use 3d mainly. the lines work for me on a basic cube that isnt moving around
Yea probably
I think the rb.rotation value is directly changing the length of the vector distance
haha
should I use this in that case?
Vector3 rightDirection = new Vector3(Mathf.Cos(rb.rotation * Mathf.Deg2Rad), Mathf.Sin(rb.rotation * Mathf.Deg2Rad), 0);
Vector3 upwardDirection = new Vector3(Mathf.Sin(rb.rotation * Mathf.Deg2Rad), Mathf.Cos(rb.rotation * Mathf.Deg2Rad), 0);
ah 2d sucks ass. ok i think this should work
Quaternion quatRotation = Quaternion.AngleAxis(rb.rotation, Vector3.forward);
you can use trig and stuff but i dont like to
should I be using Vector3.right and up? I think forward is towards the screen in 2d
ah maybe I'm speaking non sense
This would just be simply getting the rotation in terms of a quaternion. Really you dont even need the forward direction since u arent using it
theres a ton of ways to calculate the directions but i just know it from heart based on quaternion math.
Quaternion quatRotation = Quaternion.AngleAxis(rb.rotation, Vector3.forward);
Vector3 rightDirection = quatRotation * Vector3.right;
Vector3 upDirection = quatRotation * Vector3.up;
Is this correct?
should be fine
And my ground functions
public bool isGrounded()
{
Vector3 groundCheckPosition = rb.position + new Vector2(0, -0.445f);
return Physics2D.OverlapBox(groundCheckPosition, new Vector2(0.57f, 0.025f), 0, groundMask);
}
public bool isSloped()
{
Vector3 groundCheckPosition = rb.position + new Vector2(0, -0.445f);
return Physics2D.OverlapBox(groundCheckPosition, new Vector2(0.57f, 0.35f), 0, slopeMask);
}
yea should be good as well
oh wait the offset needs to take into account the rb angle haha
maybe you also do want to provide an angle for that box, but i dont think that'd specifically be an issue considering that isnt frame rate dependant. the position of the overlap box would be the frame rate dependant part
oh yea sorry, it has to use the -up
Like this? 😅
public bool isGrounded()
{
Quaternion quatRotation = Quaternion.AngleAxis(rb.rotation, Vector3.forward);
Vector3 upDirection = quatRotation * Vector3.up;
Vector3 groundCheckPosition = rb.position + -upDirection * new Vector2(0, -0.445f);
return Physics2D.OverlapBox(groundCheckPosition, new Vector2(0.57f, 0.025f), 0, groundMask);
}
ignore -0.445 it should be positive
you would want that 0.445f to be just a number, not as part of a vector. Im not even sure that line would compile because u cant multiply vectors
oh alright haha
i would also just pass the up direction as a parameter, but for the time being just see if it works
because its already being used in fixed update, so no need to recalculate it
fair enough
If I'm not mistaken, is that what your suggest changes would look like?
https://paste.ofcode.org/JWwWcRv2cJMSgXjtFNsmTU
uh looks alright but just to be safe you could debug what that position is. Even just by doing
Debug.DrawRay(rb.position, -upDirection * 0.445f). And maybe put those
-upDirection * 0.445f in brackets so you are sure about the order of operations
I ran the game, now, the different frame rates aren't 100% equal, but it does seem significantly closer to one another, to the point I think it may not frame dependent?
Looks a lot closer at least yea
It also seems to be deterministic now? Running it multiple times, the different frame rates seem to giving the same results
the same slightly different results albeit
Try double checking those directions because i didnt fully go through to ensure the math was correct. One thing could simply be because you have interpolate on. this could cause a difference in what the top height is but i do see even in one run that cube is rotating slightly
Alright
the ray seems correct
given transform.position is different to rb.position, when pausing the same, this different seems to correspond to the ray's offset from transform.position
i meant more like check the groundCheckPosition, but honestly this might just require a check of every single value if you want to dig deeper into the differences.
Id print out to file a bunch of values, maybe even every 50 fixedupdates (use a counter). then run at 240 fps and print to a different file
because im too tired to actually go through that math again 😅
Given how small these differences seem to be, I'd be willing to call that the inevitable frame rate physics difference and go with it
It seems to me that it's barely at all frame dependent, and I'd call that a huge win
I can't thank you enough for your help
It's weird that only this particular aspect of the physics in my original project had such noticeble frame dependency
like I'd test other aspects from a set potion, and the difference in the positions of the end results were so fractionally small
just gotta hope I can successfully incorporate the changes you showed me into my original project
I didnt fully go through the logic of why one would launch higher, but its most likely because one frame rate would move the object in a way so that it didnt appear grounded or sloped.
Which likely caused it to launch earlier/later or whatever
Oh yea just one question
In tutorials that can be found in Youtube, I've never seen such in depth logic be necessary for a ground check
Would that mean this rb.position code, which to me I've never seen used in this way and is new to me, is really only needed for like, really keeping things organised and to make sure things to properly
Sorry if my words don't make too much sense in what I'm saying, english is my second language
well i often say 99% of tutorials are complete dogshit and work only for the duration of the video. It cant be extended further. This is proof why
Most of them dont know what they're doing, and do what works visually at first. Then pump out the videos
Also, huge props to you, you took like an hour out of your day just to help a stranger fix a niche bug
🙏Huge thanks
realistically you should be using it, if you want to respect proper physics. There are definitely cases you dont need it, like in my game i move via physics and have child objects that indicate where the player should spawn projectiles. I just use the childObjectName.position and yea there will never be a noticeable difference unless some nerd comes along, decompiles/mods the game to check, and points it out
np, usually things like these help me learn too which is a big reason i even really look in these channels
i hope my point above about where its not needed is clear though
Haha I think it makes sense
the physics in my original game behaved as close as I could make it to the Sonic platformers back in the 90s, which I guess could get quite maths heavy or complicated at times, causing it to the need the indepth code you showed me
Well, my code is very different to the way they did it in the 90s, but the behaviour is similar anyway, just achieved through a supposedly stranger manner
even for me its hard to really suggest cases where you should use rb.position or where you can get away with transform.position. I think from this day though you can probably see its use case. Basically if something actually relies on where the rigidbody is then you need it. But knowing what "relies on where the rigidbody is" might be confusing
Given the other aspects of my original project seem to work just fine, I'll probably give this particular aspect of the physics its own ground check code, otherwise I'd be retesting everything else to make sure it still works
Once again, huge thanks!
Is there a karma system in this server? I don't know if I should be thanking you in a specific way to add to that, or if helping is just for the sake of helping really.
no karma system, just helping 🙂
i learned at least 90% of what i know, rotations and that stuff, from just helping/seeing other people help and trying things out myself based on it