Hey. There is such a script:

float localHeight = CharHeight; if (_PlayerMode == PlayerMode.Crouch) { localHeight = CharHeight * 0.5F; } float lastHeight = Controller.height; Controller.height = Mathf.Lerp(Controller.height, localHeight, 5 * Time.deltaTime); CharTransform.position = new Vector3(transform.position.x, transform.position.y + (Controller.height - lastHeight) / 2, transform.position.z); //Fix vertical position CharTransform = transform. 

I think many people had a problem that with this method of squatting, the camera did not move to the end, but half from the height of Character `a. It turns out, you need to do animation for the camera? And when creating an animation to take into account that it will still move a little?

  • and if in Russian? what squats? where does the squat? ΠΏΡ€ΠΈ Ρ‚Π°ΠΊΠΎΠΌ ΠΌΠ΅Ρ‚ΠΎΠ΄Π΅ Ρ€Π΅Π°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ присСданий, ΠΊΠ°ΠΌΠ΅Ρ€Π° двигалась Π½Π΅ Π΄ΠΎ ΠΊΠΎΠ½Ρ†Π° - which end? Where did you go? - Alexey Shimansky
  • As can be seen from the script, squatting is a change in the height of the controller. When the controller crouches (changes its height), the camera does not move down completely. In general, if it is not clear, I can record a video. - Edward
  • If you want help, describe your entire question. Nothing is visible from the script except what is written there. Neither the camera is fixed, nor what is the β€œend” to which it should move, nor what is a squat, nothing. In addition, no one wants to read into someone else's code, not knowing why. - ANU CHEEKI BREEKI
  • Then here is youtu.be/8ZCK2K1re7A - Edward
  • In the standard state, the camera is in Capsule Collider, but when I execute the script, it moves beyond the controller a little bit and eventually goes beyond the edges of the calider. In the hierarchy, the camera is attached to the controller. - Edward

2 answers 2

enter image description here

  1. Character to squat. The camera is tied to the top of the capsule.
  2. Squeezing the capsule on the character. The capsule is still at the top of the old position.
  3. Lowering the character to the ground so that the pivot touches the base of the capsule again (25% of the capsule). The camera is lowered by the same amount. That is, the camera is lowered by 25% of the capsule instead of the desired 50% (half of the capsule)
  4. That is how it should be. The camera must also be lowered by 50%, not 25%.

The same is obvious to the naked eye. Shift the center of the capsule, not the pivot, and the camera is supported over the upper border of the capsule.

  • Thank you very much! - Edward
  • Something like this: `Controller.center = new Vector3 (Controller.center.x, Controller.center.y + (Controller.height - lastHeight) / 2, Controller.center.z); `? With this option, the controller also smoothly changes growth, but the camera does not change position at all. Center Y goes to minus. "The camera is supported over the top of the capsule." I understand this code need to do? Just when I change the center through the inspector, the camera moves, and if there is no code. - Edward
  • If you think that I'm stupid or my questions are too banal, then please let me know that I would not expect an answer. - Edward
  • Everything. Understood. Added this line `PlayerCamTransfrom.transform.position = new Vector3 (PlayerCamTransfrom.transform.position.x, PlayerCamTransfrom.transform.position.y + (Controller.height - lastHeight), PlayerCamTransfrom.transform.position.z); ` - Edward

Look towards [Cinemachine][1] - a free asset from Unity. It provides many opportunities to set up a camera for the observer. There is support for dead zones, smooth observation, random jitter (for example, when held in the hands) and much more.