# ,Making Player Stay on edge of a gameobject

I am Trying to make a game where the player can stick to rotating disks and shoot off them to another.
but sometimes the player doesnt stay on the edge but to a bit far from it.
how do i solve this?
also I want it so that I will not have to set values for every disk i place and instead have the code
adapt itself to the disk it is currently stuck with
also i do sorta have a solution and that is to find the width of the current disk the Player is attached with divide by 2 and do the same for the player add them and set the players x position to that but i cant find a
to do it in code.

here is my code.

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

public class PlayerController : MonoBehaviour
{
public GameObject player;
public GameObject disk;
private bool isStart = true;
private float rotationSpeed = float.PositiveInfinity;
public float speed = 10;
public Rigidbody2D playerB;
public bool hascontrol = false;
public SpriteRenderer diskSize;
private Vector3 width;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isStart == true)
{

Mgo();

isStart = false;
hascontrol = true;
}
}

public void Mgo()
{
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);

}

public void go()
{

}

private void FixedUpdate()
{

if (hascontrol == true && Input.GetKeyDown(KeyCode.Space))
{
Mgo();
hascontrol = false;
transform.SetParent(null);
}

}

private void OnCollisionEnter2D(Collision2D collision)
{

if (collision.gameObject.tag == "Disk")
{

player.transform.parent = collision.transform;
hascontrol = true;
}
}

}
``````

As far as I understand you want so that the Player sticks to an object that spins and later is able to shoot it off the opposite direction so it can stick to another game object.

So what you will need is to get the disk gameObject component I already make this script that will need to apply to the player with the connection logic

``````using UnityEngine;

public class Player : MonoBehaviour
{
//SerializeField is use to keep the variable private but still available to see in the inspector.
[SerializeField] private GameObject diskGameobject;

//Using the RigidBody2D component into a variable.
private Rigidbody2D playerRb;

//This will be the force in with the player will be launch.
private float launchForce = 5f;

// Start is called before the first frame update
void Start()
{
// Get the RigidBody2D component from the player.
playerRb = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
//Check if the player is in a disk and when he presses space it will be lunch the opposite direction.
if (Input.GetKeyDown(KeyCode.Space) && diskGameobject != null)
{
//Detach the player from the  disk that is currently in.
gameObject.transform.parent = null;

//Add the force to launch the player to the opposite direction the the current disk that he's in.
playerRb.AddForce(transform.position - diskGameobject.transform.position * launchForce, ForceMode2D.Impulse);
}
}

//Trigger when the player collides with another collider.
private void OnCollisionEnter2D(Collision2D disk)
{
//Check if the player collided with a Disk tagged collider.
if (disk.gameObject.CompareTag("Disk"))
{
//Sleep the RigidBody2D so the player doesn't move from his position.
playerRb.Sleep();

//Set the variable "diskGameObject" to the new disk that the player collided with.
diskGameobject = disk.gameObject;

//Make the new disk that the player collided with a parent of player.
gameObject.transform.parent = diskGameobject.transform;
}
else
{
//If the collision isn't tagged as "Disk" then it won't work.
Debug.Log("The player collided with an object that isn't a disk or is not tagged as Disk.");
}
}
}
``````

Note that you will need to set the player to a disk by default at the start of the game so in the player script,you give it his gameObject and make it a parent!- [2]

If you have any questions about the script that I made, let me know.

Can you send an image of the edge problem?

It might just be that the collider on either the disk or the player is too big. So when both of them collide the point of collision is away from the edge of the disk…