Skip to content

Instantly share code, notes, and snippets.

@valbeat
Created October 22, 2015 09:06
Show Gist options
  • Save valbeat/7c3e7ee773a9447de779 to your computer and use it in GitHub Desktop.
Save valbeat/7c3e7ee773a9447de779 to your computer and use it in GitHub Desktop.
Unityで真上からのカメラで位置のクランプ
void Clamp() {
Vector3 posZToY = position;
posZToY.y = position.z;
Vector3 posInScreen = Camera.main.WorldToViewportPoint(posZToY);
Vector3 pos = position;
posInScreen.x = Mathf.Clamp (posInScreen.x, 0, 1);
posInScreen.y = Mathf.Clamp (posInScreen.y, 0, 1);
Vector3 _pos = Camera.main.ViewportToWorldPoint(posInScreen);
pos.x = _pos.x;
pos.z = _pos.y;
position = pos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment