Keep object rotating around a collider given a distance to keep

Hi there,

I am currently having something I know the reason, but can not easily find the solution.

I post the code:

		//vector from player to hitPoint
	Vector3 playerToHitpoint = (player.transform.position- hit.point);

//calculate distance
float currentDistance = playerToHitpoint.magnitude;

//keep the player at the desired distance
player.transform.Translate((currentDistance - sneakWallDistance) * playerToHitpoint);

My problem is, that the object can only rotate half the way around a given collider. I think it has to do with playerToHit-Vector beeing positive/negative.

Any help greatly appreciated!

There are two problems:

1- you’re using the vector reversed (it’s pointing to the player, not to the hit.point);

2- since both are world points, you must define Space.World in transform.Translate, or else the player will be translated in its local coordinate system.

//vector from player to hitPoint
Vector3 playerToHitpoint = hit.point - player.transform.position;
//calculate distance
float currentDistance = playerToHitpoint.magnitude;
//keep the player at the desired distance
player.transform.Translate((currentDistance - sneakWallDistance) * playerToHitpoint, Space.World);