- Home /
Question by
swisscoder · Sep 29, 2011 at 04:04 PM ·
distancetranslate
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!
Comment
Best Answer
Answer by aldonaletto · Sep 29, 2011 at 04:20 PM
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);
Wow, amazing! Thanks a lot! those nifty little things about all those methods can take a lot of your time away :D
Your answer
