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 Arc Christelle · Dec 31, 2013 at 12:43 AM · 2d

How to Disable Horizontal Movement in Crouch Position? (2D)

I've recently written a script to make my character crouch by pressing down. Yet when in crouched position he is still able to move left and right. Here's my code so far regarding the script. I'm new to scripting in Unity and C#, but currently learning as I go. I just want to know if I can possibly prevent the player from moving while crouching and how to place it in the if statement.

 using UnityEngine;
 using System.Collections;
 
 public class Crouch : MonoBehaviour {
     public Animator anim;
     public string axisName = "Vertical";
     public bool Neutral = false;
 
     // Use this for initialization
     void Start () {
             anim = gameObject.GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.GetAxis(axisName) < 0)
         {
             anim.SetTrigger("crouch");
             anim.SetBool("Neutral", false); //crouches down
     
 
         }
         else if(Input.GetAxis(axisName) >= 0)
         {
             anim.SetBool("Neutral", true); //returns to idle
         }
     }
 }
   
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 KellyThomas · Dec 31, 2013 at 12:46 AM 0
Share

It would be much easier to achieve if the crouch code was integrated with the general movement script.

avatar image Arc Christelle · Dec 31, 2013 at 01:10 AM 0
Share

I don't think I can since my movement script uses axisName for Horizontal movement. I created this from following the first $$anonymous$$ax and Phil tutorial, it kinda got me into the flow of things. However I'm open to change it up if there's a much better way.

 public class $$anonymous$$ove : $$anonymous$$onoBehaviour
 {
     public float speed = 1.0f;
     public string axisName = "Horizontal";
     public Animator anim;
     // Use this for initialization
     void Start () 
     {
         anim = gameObject.GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () 
     {
         anim.SetFloat("Speed", $$anonymous$$athf.Abs(Input.GetAxis(axisName)));
         if(Input.GetAxis(axisName) < 0)
         {
             Vector3 newScale = transform.localScale;
             newScale.x = -1.0f;
             transform.localScale = newScale;
         }
         else if(Input.GetAxis(axisName) > 0)
         {
             Vector3 newScale = transform.localScale;
             newScale.x = 1.0f;
             transform.localScale = newScale;
         }
         transform.position += transform.right*Input.GetAxis(axisName)*speed*Time.deltaTime;
     }
 }
 

2 Replies

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

Answer by KellyThomas · Dec 31, 2013 at 04:30 AM

One solution would work like this:

Crouch.cs

Add a public boolean variable, then update this variable on player input:

 //insert below line 4: 
 public bool isCrouched = false;

 //insert below line 18:
 isCrouched = true;

 //insert below line 25:
 isCrouched = false;

Move.cs

Add a private Crouch variable then find the instance attached to the current game object. Use this to access the boolean we created before and decide if movement will be applied.

 //insert below line 2: 
 private Crouch crouch;

 //insert below line 8:
 crouch = GetComponent<Crouch>();

 //replace line 28 with this group:
 if(!crouch.isCrouched) {
     transform.position += transform.right*Input.GetAxis(axisName)*speed*Time.deltaTime;
 }

Please note that the line numbers I'm using for reference are from the code posted above, your files may differ so check this page to see where they should go.

Comment
Add comment · Show 3 · 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 Arc Christelle · Dec 31, 2013 at 04:46 AM 0
Share

Thank you soo much! It works perfectly! I really appreciate it! Considering reworking the code in the future due this delay in the crouch (always been there since I created it). Along with trying to get it to work with diagonal inputs. But I'd like to thank you again.

avatar image KellyThomas · Dec 31, 2013 at 05:31 AM 0
Share

The crouch appears slow?

  1. Now we have exposed the isCrouched variable you can monitor it using the Inspector. If the animation lags behind the variable significantly you may need to modify the animation ti$$anonymous$$g.

  2. With the public variable approach there is the possibility that, if $$anonymous$$ove is executed before Crouch in each update pass, then it will see the isCrouched value from the frame before. This single frame delay can be fixed by rena$$anonymous$$g the Update() method for $$anonymous$$ove to `LateUpdate()`.

avatar image Arc Christelle · Dec 31, 2013 at 06:11 AM 0
Share

The LateUpdate definitely makes it more responsive there's still this slight delay once I release the button. The crouch animation is a single frame (I'm using the sprites from Castlevania NES) $$anonymous$$y goal is to make Simon rise instantly on release. (Edit: I think reversing the if and else if statements helped abit, still feel a little latency yet slight improvement on release.)

I connected the walking animation and the crouch in order to make the crouch work by diagonal input, although I'm having a hard time thinking of how to get him to return to walking animation if I release down. (Edit:I changed up the conditions in the animator and got it to work by setting up Neutral = True and Speed > 0.1 There's a weird stutter when it returns to walking animation and it flashes to crouch for a brief sec but I'll figure that out.)

Once again I thank you for your help and hope I'm not being difficult, I really appreciate it, the past couple of days of working in Unity has been the most productive for me so far.

avatar image
0

Answer by J-R-Wood · Dec 31, 2013 at 02:11 AM

Maybe add the player Control to the main camera and then when the player crouches switch to another camera which has the Z axis disabled in the inspector. vote my answer up if this works

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 Arc Christelle · Dec 31, 2013 at 02:18 AM 0
Share

I don't understand how that would work with a 2D game though since movement would take place solely on the X and Y. If I could place some code between the if statements to disable horizontal movement, that would be ideal.

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

20 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

Related Questions

Want to make a JRPG. 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Strange GOs Bounce after parenting 2D game 0 Answers

2D pong game. HELP 1 Answer

2d water simulation simulation 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