Greetings, beautiful world of Unity and all that is connected to it!
When I crouch, my weapon gets squashed! It’s really uncomfortable. I think I know why it does that (weapon is a child of the main camera, main camera is a child of the player, player gets reduced in height, so do the children.), I just don’t know how to fix it.
Here’s the code, enjoy. If you feel like I could improve this code in any other way besides the problem, I’d love to hear about it.
// Crouching
if (Input.GetKeyDown (KeyCode.C)) {
if (isCrouching == false) {
isCrouching = true;
movementSpeed = crouchSpeed;
Camera.main.transform.localPosition = new Vector3(0,crouchCameraHeight,0);
Vector3 temp = new Vector3(1,crouchHeight,1);
capsuleCollider.transform.localScale = temp;
} else {
isCrouching = false;
movementSpeed = runningSpeed;
Camera.main.transform.localPosition = new Vector3(0,standardCameraHeight,0);
Vector3 temp = new Vector3(1,playerHeight,1);
capsuleCollider.transform.localScale = temp;
}
}
Probably the best idea is to not change the scale of the player when you want to do things. Not only is changing the scale on things that have rigidbodies a bad idea, but it will also mess with the entire hierarchy.
-
I would suggest using an animation to do your crouching instead.
-
If you can’t accomplish something like that, then perhaps you should think about moving the camera and weapon to different GameObject trees and provide them with a reference to the player if it’s really needed.
I understand that it would be better to have the camera and weapon be a part of the player’s gameobject tree, so I highly suggest the first option. Don’t change the scale of the player.
Since you are scaling the parent of the weapon, the weapon will be scaled too. You could apply the inverse scale to the weapon as a quick fix. The best solution is to not scale your character. Generally speaking, you don’t want to scale objects that will potentially animate unless it is a uniform scaling, and even then it causes extra processing to unscale the animated bones (when using skinned meshes) if it works at all. Even if you don’t have an animation or real mesh for your character right now, your current design won’t allow for adding it later.
Since you don’t have one now, you can just move the gun and camera separately instead of scaling the player. Create an empty game object, put it at the center of your character’s current game object root. Then, move the gun and camera into that new game object. Now when you want to move the camera down while still allowing the player to interact with the terrain, you can just move the position of this new game object down when crouching and back up when you’re done crouching. Just put any objects attached to the player as children of this game object and they will move with the crouch.
Thank you all for the help. Unfortunately, my computer went on a blue screen streak, so it might be a while before I have everything set up again (luckily I did manage to salvage the most important stuff). Since only one correct answer can be accepted, I decided to close my eyes and picked one, because I couldn’t choose. As soon as everything’s working again, I’m going to look into the things you guys suggested!