Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 spicypeachdev · Aug 21, 2020 at 07:01 PM · 2dmovementplayermovement scriptplayer movement

Player won't stop moving while blocking

In my script, I have it so that when the player presses 'B' on the keyboard the in-game character blocks an attack. When I first implemented the mechanic using script, the player could move while blocking which was something I didn't want, so I fixed it by turning the movement and jump to 0 in the instance that the player was pressing 'B'. And that worked just fine until now. For some reason after I had added another mechanic that allows the player to shoot projectiles, the player is now able to move while blocking, even though the script is telling them not to. The problem really only lies with the left-right movement since the player isn't able to jump.

I've tried increasing the gravity and lowering the movement passed 0 =, but to no avail.

Here's the script that contains the player's blocking and projectile mechanic:

{

 public float maxSpeed = 10f;
 public float jumpHeight = 45f;
 public float gravityScale = 11f;
 private Animator anim;



 float moveDirection = 0;
 bool isGrounded = false;
 Vector3 cameraPos;
 Rigidbody2D rb;
 Collider2D mainCollider;
 // Check every collider except Player and Ignore Raycast
 LayerMask layerMask = ~(1 << 2 | 1 << 8);
 Transform t;

 // Use this for initialization
 void Start()
 {
     t = transform;
     rb = GetComponent<Rigidbody2D>();
     mainCollider = GetComponent<Collider2D>();
     rb.freezeRotation = true;
     rb.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
     rb.gravityScale = gravityScale;
     gameObject.layer = 8;
     anim = GetComponent<Animator>();
 }

 // Update is called once per frame
 void Update()
 {
     // Movement
     if ((Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) && (isGrounded || rb.velocity.x > 0.01f))
     {
         moveDirection = Input.GetKey(KeyCode.A) ? -1 : 1;
     }
     else
     {
         if (isGrounded || rb.velocity.magnitude < 0.01f)
         {
             moveDirection = 0;
         }
     }

       
     if (Input.GetKeyDown(KeyCode.W) && isGrounded)
     {
         rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
     }

     //DEFENSE
     if (Input.GetKey(KeyCode.B))
     {
         anim.SetBool("isBlocking", true);
         maxSpeed = -100f;
         jumpHeight = 0f;
         
     }
     else
     {
         anim.SetBool("isBlocking", false);
         maxSpeed = 10f;
         jumpHeight = 45f;
     }

     //ATTACKS

     //Simple Freeze
     
     if (Input.GetKey(KeyCode.F))
     {
         anim.SetBool("isBlasting", true);
     }
     else
     {
         anim.SetBool("isBlasting", false);
     }


 }

 void FixedUpdate()
 {
     Bounds colliderBounds = mainCollider.bounds;
     Vector3 groundCheckPos = colliderBounds.min + new Vector3(colliderBounds.size.x * 0.5f, 0.1f, 0);
    
     isGrounded = Physics2D.OverlapCircle(groundCheckPos, 0.23f, layerMask);

     
     rb.velocity = new Vector2((moveDirection) * maxSpeed, rb.velocity.y);

 }

}

Not sure if it's needed, but here's an additional script for the projectile:

{ public Rigidbody2D iceBlast; public float iceBlastSpeed;

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.F))
     {

         StartCoroutine(Wait());
        
     }
 }

 IEnumerator Wait()
 {
     yield return new WaitForSeconds(.3f);
     
     var iceBlastInst = Instantiate(iceBlast, transform.position, Quaternion.Euler(new Vector2(0, 0)));
         iceBlastInst.velocity = new Vector2(iceBlastSpeed, 0);

 }

 

}

Feel free to ask questions, and thank you for the help.

Comment
Add comment · Show 1
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 Eno-Khaon · Aug 21, 2020 at 07:47 PM 0
Share

For reference, I'd like to note that your character specifically moves when blocking in response to these lines:

 // ...
 maxSpeed = -100f;
 // ...

and

 // ...
 rb.velocity = new Vector2((moveDirection) * maxSpeed, rb.velocity.y);
 // ...


Without any other handling, your movement speed is multiplied by your maximum speed, regardless of whether it's positive or negative. You can solve the problem by setting maxSpeed to 0f when blocking, but it might help to research more and find a more thorough solution.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Callum_0 · Aug 21, 2020 at 07:18 PM

Have you tried to use a bool to specify if the player can move?

 private bool _canMove = false;


Then just set the bool where your blocking code is:

 if (Input.GetKey(KeyCode.B))
 {
     anim.SetBool("isBlocking", true);
     _canMove = false;
 }
 else
 {
     anim.SetBool("isBlocking", false);
     _canMove = true;
 }


And in your movement code just check if _canMove is true.

 if ((Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) && (isGrounded || rb.velocity.x > 0.01f) && _canMove)
 {
     moveDirection = Input.GetKey(KeyCode.A) ? -1 : 1;
 }
 else
 {
     if (isGrounded || rb.velocity.magnitude < 0.01f)
     {
         moveDirection = 0;
      }
 }
Comment
Add comment · Show 7 · 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 spicypeachdev · Aug 21, 2020 at 07:41 PM 0
Share

Thank you for the reply, but that didn't seem to work, unfortunately.

avatar image Callum_0 spicypeachdev · Aug 21, 2020 at 07:51 PM 0
Share

Change this:

 if ((Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) && (isGrounded || rb.velocity.x > 0.01f))
 {
     moveDirection = Input.GetKey(KeyCode.A) ? -1 : 1;
 }
 else
 {
     if (isGrounded || rb.velocity.magnitude < 0.01f)
     {
         moveDirection = 0;
      }
 }

To this:

 if (isGrounded || rb.velocity.x > 0.01f)
 {
     if (Input.GetKey(KeyCode.A)
             moveDirection = -1;
     else if (Input.GetKey(KeyCode.D)
             moveDirection = 1;
     else
             moveDirection = 0;
 }

Then inside your FixedUpdate method do this:

 if (_can$$anonymous$$ove)
 {
     rb.velocity = new Vector2((moveDirection) * maxSpeed, rb.velocity.y);
 }

avatar image spicypeachdev Callum_0 · Aug 21, 2020 at 08:21 PM 0
Share

That's not working either. I'm not really sure what the problem is.

Show more comments
Show more comments

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

350 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 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 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

Non slippery movement 1 Answer

Player movement boudaries in 2D 1 Answer

How can I make a collision sensitive teleporter for a 2D game #c 1 Answer

Help with my Dash move? 1 Answer

How to make an object move to another object's location in a 2d game? 2 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