# Flying around an edge of a Cube

So the player drags and releases object (red circle) like in angry birds. On all 4 sides of the cube the object
follows the flat surface of the cube (imagine top and bottom of the cube are infinitely long). When the red circle reaches an edge it should fly within a curve (red line) to the next side of the cube based on its direction and velocity. To put it simple it is like in Mario Odyssey where Pokio (The bird with the long spike) can fly around edges.

My problem is that I do not know how to get the red circle from one side to the other. I used raycast, gravity and bezier curves but nothing worked out for me.

public class PlayerMovement : MonoBehaviour
{
public CameraMovement camMove;

``````    public float launchSpeed;

[SerializeField]
private float treshold = 0.35f;
[SerializeField]
private float adjusting = -3.0f;
[SerializeField]
private float maxDragDelta = 2.0f;
[SerializeField]
private float shaking = 1.0f;

private Rigidbody rb;
private Vector3 hook;
private Vector3 direction;
private Vector3 force;
private Animator idle;

private float distance;
private bool started;
private float calculatedDistance;

private enum State { PAUSE, DRAGGING, FLYING }
private static State playerState;

void Awake()
{
//idle = GetComponent<Animator>();

playerState = State.PAUSE;
//IdleAnimation();

rb = GetComponent<Rigidbody>();
started = true;
calculatedDistance = adjusting - Camera.main.transform.position.z;
}

void Update()
{
/*if(Input.touchCount == 1)
{
switch (Input.GetTouch(0).phase)
{
case TouchPhase.Began:
InitializePosition();
break;
case TouchPhase.Moved:
Vector3 touchPos = Input.GetTouch(0).position;
SetPosition(touchPos);
break;
case TouchPhase.Ended:
CheckDistance();
//camMove.SwitchStates(IsDraging());
break;
}
}*/

if (hasStarted())
{
Vector3 mousePos = Input.mousePosition;
if (Input.GetMouseButtonDown(0))
{
InitializePosition();
}
else if (Input.GetMouseButton(0))
{
SetPosition(mousePos);
}
else if(Input.GetMouseButtonUp(0))
{
if (playerState.Equals(State.DRAGGING))
{
CheckDistance();
//camMove.SwitchStates(IsDraging());
}
}
}

/* rotate stack based on drag angle and player speed */
/*if (playerState.Equals(State.FLYING))
{
Flying();
}*/
}

private void InitializePosition()
{
playerState = State.PAUSE;
//IdleAnimation();

rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;

rb.useGravity = false;
hook = transform.position;

if (!playerState.Equals(State.FLYING))
{
playerState = State.DRAGGING;
//IdleAnimation();

//camMove.SwitchStates(IsDraging());
}
else
{
playerState = State.PAUSE;
}
}

private void SetPosition(Vector3 playerPos)
{
playerPos.z = calculatedDistance;
Vector3 newVector = Camera.main.ScreenToWorldPoint(playerPos);
newVector.z = adjusting;
//transform.LookAt(hook);

distance = Vector3.Distance(newVector, hook);
direction = (newVector - hook).normalized;

if(distance >= maxDragDelta)
{
rb.position = hook + direction * maxDragDelta;
distance = maxDragDelta;

//shake player
Vector3 newPos = Random.insideUnitSphere * (Time.deltaTime * shaking);
newPos.x = rb.position.x + newPos.x * direction.x;
newPos.y = rb.position.y + newPos.y * direction.y;
newPos.z = rb.position.z;
rb.position = newPos;
}
else
{
rb.position = newVector;
}
}

private void CheckDistance()
{
transform.eulerAngles = Vector3.zero;
if (distance >= treshold)
{
playerState = State.FLYING;

rb.useGravity = true;
force = direction * distance * launchSpeed;
rb.AddForce(-force, ForceMode.Impulse);
}
else
{
playerState = State.PAUSE;
//IdleAnimation();

rb.position = hook;
}
}

private void Flying()
{

}

private void IdleAnimation()
{
if (playerState.Equals(State.PAUSE))
{
idle.SetBool("idle", true);
}
else
{
idle.SetBool("idle", false);
}
}

public Vector3 getDirection()
{
return direction;
}

public bool hasStarted()
{
return started;
}
}
``````