Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Tyr Lothor · Feb 07, 2014 at 08:37 PM · c#weaponfirst personcrouching

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;
             }            
         }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scribe · Feb 07, 2014 at 09:16 PM 2
Share

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.

avatar image robertbu · Feb 07, 2014 at 09:45 PM 1
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

  1. I would suggest using an animation to do your crouching instead.

  2. 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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image supernat · Feb 07, 2014 at 09:50 PM 1
Share

Beat me by 7 seconds! :)

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges