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 /
  • Help Room /
avatar image
0
Question by Alcatrazuelo · Mar 21 at 12:34 AM · 2d gamemovement scriptforceplayer movementdash

[2D] How to stop 2 vectors from adding force on each other

Hello everyone, this is my first time working with code and Unity, so to get familiarized with the workflow I'm recreating mechanics from retro games, starting with Alucard's moveset from Symphony of the Night. The problem is with his backdash adding up with the force of the walk, if I'm pressing right and backdash, then it does the backdash, but slower and if I'm pressing left and backdash it goes faster. Here's my code so far:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
 
     [Header("Walking")]
     public float WalkSpeed = 10f;
     public float JumpForce = 12f;
     public float GroundDetectionRadius = 0.25f;
     [Header("BackDash")]
     public float DashForce = 10f;
     public float DashDuration = 1;
     public float DashCD = 1;
 
     [SerializeField] private LayerMask JumpableGround;
 
     private Rigidbody2D rbody;
     private SpriteRenderer sprender;
     private BoxCollider2D bcoll;
     float Xinput;
     bool IsGrounded;
     bool jump;
     bool isDashing;
     bool CanDash = true;
     IEnumerator DashCor;
 
     private void Start()
     {
         rbody.gravityScale = 3;
       
     }
     private void Awake()
     {
         rbody= GetComponent<Rigidbody2D>();
         sprender= GetComponent<SpriteRenderer>();
         bcoll= GetComponent<BoxCollider2D>();
       
     }
 
     private void Update()
     {
         IsGrounded= Physics2D.BoxCast(bcoll.bounds.center, bcoll.bounds.size, 0f, Vector2.down, .1f, JumpableGround);
         Xinput =Input.GetAxisRaw("Horizontal");
         rbody.velocity=new Vector2(Xinput * WalkSpeed, rbody.velocity.y);
 
         if (Input.GetButtonDown("Jump") && isGrounded())
         {
             jump = true;
         }
         if(Input.GetButtonDown("Dash") && isGrounded() && CanDash==true)
         {
             if (DashCor != null)
             {
                 StopCoroutine(DashCor);
             }
             DashCor = Dash(DashDuration, DashCD);
             StartCoroutine(DashCor);
         }
     }
 
     private void FixedUpdate()
     {
         if (IsGrounded && jump)
         {
             rbody.AddForce(Vector2.up * JumpForce, ForceMode2D.Impulse);
             jump = false;
         }
         if (isDashing && IsGrounded)
         {
             rbody.AddForce(Vector2.left * DashForce, ForceMode2D.Impulse);
         }
     }
     private bool isGrounded()
     {
         return Physics2D.BoxCast(bcoll.bounds.center, bcoll.bounds.size, 0f, Vector2.down, .1f, JumpableGround);
     }
 
     IEnumerator Dash(float DashDuration, float DashCD)
     {
         isDashing=true;
         CanDash = false;
         yield return new WaitForSeconds(DashDuration);
         isDashing = false;
         yield return new WaitForSeconds(DashCD);
         CanDash = true;
     }
 }


Thanks everyone!

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

0 Replies

· Add your reply
  • Sort: 

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

233 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

Related Questions

Move Player along normal of floor? 1 Answer

Player Movement Freezes in Unity3D [Please help, no answers!] 1 Answer

2D player sliding after scene transition 0 Answers

MoveTowards not moving accurately. Getting stuck on target. 2 Answers

How To Do Basic 2D Movement? 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