- Home /
How can I smoothly rotate around the edge of a cube?
At first I give short explaination of my setup for a better understanding.
The user drags the player around a hook at one side of the cube (left, right, back or front). When he releases, the player flies in the opposite direction, where the user has dragged him. If the player reaches an edge he rotates around the edge and flies along the next side, based on the player's velocity (Rigidbody). During flight the gravity is turned on.
My Problem: I do not know how to get a smooth transition between one side and the other, if the player passes an edge. I am using Raycast right now. The problem I have is, that the player immediately snaps to the new side, but does not curve the edge. I also have not considered the rigidbody velocity of the player in the script.
Here is my code so far:
`// the center of the stack
Vector3 target = new Vector3(center.position.x, transform.position.y, center.position.z);
Vector3 dirToTarget = (target - transform.position).normalized;
Ray ray = new Ray(transform.position, dirToTarget);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, distance))
{
Debug.DrawRay(ray.origin, hit.point, Color.green);
}
else
{
Debug.DrawRay(ray.origin, ray.direction * 10, Color.red);
}
Vector3 offsetVector = hit.point - ray.direction.normalized * offset;
transform.position = offsetVector;
transform.LookAt(target);`
The method Flying, which you can see above, is called every frame. I permanently shoot a ray from the player to the center of the stack(cube). When the player crosses one side of the cube the hit.point obviously changes as well. The player is then positioned to the new hit.point minus an offset vector so that the player doesnt directly stick at the wall.
If you know any better aproach I would be delighted as well.
Below you can see a drawn image of my setup for better imagination.
Youtube Link: The actual game mechanic, which I want to achieve. Watch at 4.57min.