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
0
Question by Sam_da_dev · Jul 29, 2020 at 07:15 PM · scaleplatformercrouchingrescale

My character rescales under any object

Hi, i am trying to make a simple platformer and my character is a little square, the game is going very good but there is a problem with crouching, the crouching is going fine but when i leave my crouching button (s, down) while i am under an object, The slow speed and the ability to crouch is still there but the square re-scales

alt text

Here is the code :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
 
     public CharacterController2D controller;
 
     public float runSpeed = 40f;
     public float crouchSpeed = 30f; 
 
     bool jump = false;
     bool crouch = false;
 
     public float horizontalMove = 0f;
 
     // Update is called once per frame
     void Update()
     {
         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
 
         if (Input.GetButtonDown("Jump"))
         {
             jump = true;
         }
 
         if (Input.GetButtonDown("Crouch"))
         {
             crouch = true;
             transform.localScale = new Vector3(0.635f, 0.2559275f, 1);
         } else if (Input.GetButtonUp("Crouch"))
         {
             crouch = false;
             transform.localScale = new Vector3(0.635f, 0.613f, 1);
         }
     }
 
     void FixedUpdate ()
     {
         controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
         jump = false;
     }
 
 }
 

capture.jpg (9.4 kB)
Comment
Add comment
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

1 Reply

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

Answer by Headrush95 · Jul 30, 2020 at 12:56 AM

@Sam_da_dev you need to check if you are squished basically, to do this you need to add the blocks above to a layermask. in the inspector, top right when the above block is selected you will see a box that says "layer", create a new layer called ceiling and assign all the overhead obstacles to it.

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class PlayerMovement : MonoBehaviour
  {
  
      public CharacterController2D controller;
  
      public float runSpeed = 40f;
      public float crouchSpeed = 30f; 
  
      bool jump = false;
 
      bool crouch = false;
  
      public float horizontalMove = 0f;
 
     public bool isSquished;
     
     
     public float celiDist = 0.5f 
     
     public LayerMask celi;
     
     void Update()
     {
     isSquished = physics2D.OverlapCircle(transfrom.position, celiDist, celi);

 horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
  
          if (Input.GetButtonDown("Jump"))
          {
              jump = true;
          }
  
      // so here you would say 
     
     if (Input.GetButtonDown("Crouch"){
     
     crouch = true;
  transform.localScale = new Vector3(0.635f, 0.2559275f, 1);
     
     } else if (Input.GetButtonUp("Crouch") && !isSquished){
     
     crouch = false;
 transfrom.localScale = new Vector3(0.635f, 0.613f, 1);
     }
 
 }
 
  void FixedUpdate ()
      {
          controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
          jump = false;
      }
  
  }
 
 

i've been coding for around 2 months and only on 3D games but im pretty sure this should work. please let me know

Comment
Add comment · Show 2 · 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 Sam_da_dev · Jul 30, 2020 at 02:47 PM 0
Share

It worked, i also had to change the Z position of the square to 1, thank you for the help

avatar image Headrush95 Sam_da_dev · Jul 30, 2020 at 04:39 PM 0
Share

good I'm glad man happy dev-ing

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

136 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input Axis Vertical is not set up? 1 Answer

Is there a better way to simulate a vacuum or kirby like suction that doesn't kill frame rate? 1 Answer

Do I need to use tiling on sprite game object? 1 Answer

2D 360 degress platformer example needed 0 Answers

In 2D I'm having no luck crouching using Raycasts and transform.localScale, transform.localPosition and a for loop. Help? 0 Answers


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