- Home /
Crouching weapon, hidden solution. Weapon gets squashed when crouching.
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;
}
}
firstly, nice title! :D
So this is probably not the neatest answer which is why im adding a comment, can you get the weapong gameObject/transform in this script somehow, if so you can just set its scale to (1/crouchHeight) or 1 I'm not able to check how childs local scales are effected.
Put an empty game object in place of the weapon. Add a script to the weapon such that it follows the position and rotation (but not the local scale), of the empty game object. Also if the weapon is an immediate child of the character or if the character is the root transform, @Scribe's suggestion would be easy to implement.
Answer by supernat · Feb 07, 2014 at 09:49 PM
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.
Answer by stevethorne · Feb 07, 2014 at 09:49 PM
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.
Answer by Tyr Lothor · Feb 08, 2014 at 10:26 AM
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613132843im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I stop my weapon from firing while reloading? 2 Answers
Renderer on object disabled after level reload 1 Answer
Weapon Switching 1 Answer