How can I make flipping object position using Raycast 2d ?

Hi!

I want make a 2d side scrolling, where you’re flying to the right side direction (camera focused on the Game Object) and you can switch / flip that object to under floor/ ground line (with changing gravity of course). But I have a little struggle with making that correctly…
Im trying use Raycast2D which shoots the ray to the down (Vector2.down / -Vector2.up) and hitting the ground line. Than I get the hit.point of that raycast as a Vector2. I have also the startPoint which is just a gameObject.transform.position (beggining point of a raycast). So I made a statement - If Object get switched, set his position y to: gameObject.transform.position - newPos (new Pos is just a = startPoint - hit.point , → its the distance) but it doesn’t work :confused: … Mostly the gameObject spawns in the center point of the screen…

This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BikeControl : MonoBehaviour
{
public bool Positive_Reality;
Vector2 endCast;
Vector2 startCast;
Vector2 newPos;

void Start()
{
Positive_Reality = true;
}

void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (Positive_Reality) FlipBicycleDown();
else FlipBicycleUp();

Positive_Reality = !Positive_Reality;
}
}

private void FixedUpdate()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down);

startCast = transform.position;
endCast = hit.point;
newPos = startCast - endCast;

Debug.Log("startCast" + transform.position);
Debug.Log("endCast" + hit.point);

Debug.DrawLine(transform.position, hit.point, Color.red);

}
void FlipBicycleDown()
{

gameObject.transform.position = new Vector2(0.0f, transform.position.y - newPos.y);
gameObject.transform.eulerAngles = new Vector3(-180, 0.0f, 0.0f);
gameObject.GetComponent<Rigidbody2D>().gravityScale = -30;

}

void FlipBicycleUp()
{
gameObject.transform.eulerAngles = new Vector3(0, 0.0f, 0.0f);
gameObject.GetComponent<Rigidbody2D>().gravityScale = 30;
}

}

Please read this post for how you can add code nicely on the forums (perhaps you can edit your post so it’s easier for viewers to read): Using code tags properly

Thanks, I tried edit my post may times, but there was an error… I’ll fix that and keep in mind in future posts :).