# Moving object around long cube

So I am trying to implement a game mechanic in Unity3D, where the player can drag an object freely (in 2D space). When he releases the object, it flies in the opposite direction the player has dragged. If the object reaches an edge it should smoothly fly around. Imagine the fly-path like a cube but with rounded corners, whereas the cuboid (long cube) has sharp edges. Gravity of the player should be turned on, so the object can fall down. To put it simple the game mechanic should be similiar to POKIO in Mario Odyssey.

What have you tried so far? What part are you having trouble with? You’ve basically just described a somewhat complex game mechanic. Are you just hoping someone will write all this code for you?

If you’re struggling with where to begin with this mechanic, break it into small pieces. Learn how to move an object along a vector in space. Then maybe learn to detect when you’ve reached a branch/corner. Then learn to change velocity to the new vector. Maybe. I kind of understand what you’re looking for, but you’ve also confused me a bit, because you’ve specifically said “around long cube”, implying 3D, but you’ve also said “in 2D space”, so I don’t know if this is 2D or 3D.

Sry for my inaccurate summary. It’s gonna be a 3D game. 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 (player) reaches an edge it should fly within a curve (red line) to the next side of the cube based on its direction and velocity. 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.
Below, you can read my code so far

``````public class PlayerMovement : MonoBehaviour
{
public CameraMovement camMove;

public float launchSpeed;

[SerializeField]
private float treshold = 0.35f;
[SerializeField]
[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;
}

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());
}
}
}

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);
//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;
}
else
{
playerState = State.PAUSE;
//IdleAnimation();

rb.position = hook;
}
}

private void Flying()
{
/*RaycastHit hit;
Ray ray = new Ray(transform.position, Vector3.forward);
float range = 3.0f;

if (Physics.Raycast(ray, out hit, range))
{
if (hit.collider.tag.Equals("Wall"))
{
//hit.gameObject.GetComponent<EnemyHealth>().isHit = true;
}
}*/
}

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;
}
}
``````

At 4.57min you can see the ‘fly-path’

Well, that doesn’t look like anything is necessarily happening other than having the camera orient to keep two targets in the shot. It can’t tell what inputs the player was entering in that video clip to either A) aim their character back towards the wall again after going around the corner, or B) swivel the camera around to face that new surface. It seems like the character might be getting some aim assist to attach to the nearest surface? As for the camera, it seems that the camera tries to keep the player and their attach point in the shot?

Anyway, for the camera work, if you want something like this, I’d just use cinemachine. I would definitely not try to write my own camera follow script when cinemachine is so feature rich.

As for attaching to things, again, I don’t know exactly what the player was doing to control this video. Do you need to aim at the wall in order to attach to it, Or does it just auto-aim at the nearest surface?

Thank you for the quick reply! As far as I know the player neither needs to control the camera nor the player after he releases the player. Everything is done by code or cinemachine like you said. The first input of the user would be to press the screen to attach the player to the wall again. The important thing is that the player smoothly circuits the edge like in the video. I think auto-aim at the the nearest surface would quit fit the scenario.

I’d take a look at cinemachine’s Target Group. That might be what you want to keep two locations in view at once (the player and the aim target).

For auto-aim, I think you could do something with Physics.OverlapSphere repeatedly using a wider and wider radius to test for nearby collisions. That should give you the position of the nearest surface, more or less, which you could use as the attach point.