- Home /
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
}
}
}
It would be much easier to achieve if the crouch code was integrated with the general movement script.
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;
}
}
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.
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.
The crouch appears slow?
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.
With the public variable approach there is the possibility that, if
$$anonymous$$ove
is executed beforeCrouch
in each update pass, then it will see theisCrouched
value from the frame before. This single frame delay can be fixed by rena$$anonymous$$g theUpdate()
method for$$anonymous$$ove
to `LateUpdate()`.
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.
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
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
![](https://koobas.hobune.stream/wayback/20220613125206im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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