Sorry for bothering you with my questions… I started using Unity a few days ago, and I am facing something hard to understand.
I have a GameObject “Player” with a Transform, a BoxCollider2D, and a Sprite Renderer attached.
Every frame, I check for collisions with a simple Raycasting code then translate the object.
I check for collisions (this part works fine) :RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Gravity.GetInstance().up * directionY, rayLength, collisionMask);
I translate the gameObject : transform.Translate(velocityRelative,Space.Self);
Now here is my problem, while my gameObject is “falling” due to gravity, the sprite and the BoxCollider2D are “falling faster” than the Transform.
At the beginning, the transform.position point (blue lines) is at the center of the Sprite :
And a few frames later :
And I can’t understand why, and I’d love to.
How are you calculating center? It seems that all of the rays you are drawing are dependent on the value of center. I would guess you have this wrong.
As a general rule a collider and sprite will always have the same position as the associated transform (unless you go to some lengthy efforts to make sure they don’t, which is technically possible, but not the problem here).
Indeed, I am calculating all rayorigins from the center (as I later intend to rotate this gameobject, it is easier to find the edge from the center).
Regarding the center calculation, I assumed that transform.position would be the center of the game object. I suspect two things :
the transform.position does not work as I thought it would, as after a few translations it is no more the center of the game object
or the debug.drawray does not work as I thought it would, so the rayorigins and all the rays are wrong
Whoops, just reread the code. This is correct, transform.position will be the pivot point of the sprite. Which by default is the center. Unless you move the pivot, it will always be the center of the sprite.
I did not change the pivot point of the sprite (as seen on the first screenshot, the blue point is at the center of the sprite).
But, clearly, after a few translate the Debug.DrawRay method draw the transform.position above the sprite…
Assuming, the transform.position value is right (it should be), the problem would be with the Debug.DrawRay method. Could it be caused by a ray not refreshed ? (the ray is displayed above because it is ray from a previous frame) I will look for problems with my DrawRay use this evening.
Problem solved : it was not a Unity issue but a simple coding mistake (I am soooo stupid ).
The sprite was moving in three steps :
Update rayCastOrigins (ie. corners), and draw rays
Check for collision
Move the sprite
So, the rays were drawn at step 1 using the initial position of the sprite. Each frame were displayed : the rays in the initial position of the sprite and the sprite with its new position.
A cleaner process would be, each frame :
Cast rays and check for collision (and draw rays for the current Update call)
Move the sprite
Update corner positions (and draw rays for the next Update call)
Thank you Kiwasa for your help and confirming how transform.position works !