Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by philpq12 · Jan 22, 2019 at 03:01 AM · charactercontrolleraddforcedashmomentum

Character Controller to create a lateral dash (1st person) - Keep Momentum by Adding Force?

Hi, hopefully somebody can help me with my problem.

My main goal is to make my 1st Person Character Controller to dash sideway (quick lateral movement) when double taping A or D (within a timeframe). That said, i'm using the character controller component in order to move my player.

So far, I've been able to make that dash move that I want by increasing the speed(by multiplying with a sort of dashSpeedMultiplier) of my moveVector.x that is applied into my CharacterController.Move method. Unfortunately, this vector.x takes into account my Input.GetAxis("Horizontal"). This means that my dash cancels out as soon as I relase my A or D key. If I want to make a complete dash move like intended, I need to hold my A or D key until I reach the maximum distance possible with this increasing speed. When I release A or D in the middle of the dash. It cancels out mid air.

Here is a video of the behaviour I'm talking about. I would like the dash to feel like in the beginning of the video, not a the end where it sort of cancels out as I release the key in "midair" of my dash.

https://imgur.com/a/RsQZVe3

What I would like is whatever the A or D control is held or release, the dash should always feel the same (like if holding, even though we don't). It's like it should not take into account the Input.GetAxis,as when releasing the key, this would turn the speed to 0.

You can see below my script for reference. I have tried different things, like adding a smoothdamp to my moveVector.x (my goal was to gradually decrease the value of my Input from 1 to 0. This didn't help, maybe because I didn't use it well but overall it is affecting all my movement in a unintended way.

I tried to add a bool when strafin, and have two type of vector to put in the _controller.Move(), but it was not working too. It feels like manipulating the speed of my player may not be the best way.

Is there any way to add a force that would "push" my player on his side for creating the dashing behaviour. While this force is applied, I could lock the movement of my player until a specified time or until the controller is grounded (if for example, the player is dashing off a cliff, I would still like to keep the momentum until he's touching the ground).

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMove : MonoBehaviour
 {
 
     private CharacterController _controller;
 
     private float _verticalVelocity;
     [SerializeField] private float _gravity = 14.0f;
     [SerializeField] private float _jumpForce = 10.0f;
     [SerializeField] private float _speed = 4.0f;
     [SerializeField] private float _strafeJump = 2.0f;
     [SerializeField] private float _strafeSpeedMultiplier = 6.0f;
     [SerializeField] private float _damping = 1.0f;
 
     private bool _canDoubleJump = false;
     private bool _canStrafeLeft = false;
     private bool _canStrafeRight = false;
 
     Vector3 moveVector = Vector3.zero;
     Vector3 strafeVector = Vector3.zero;
 
     // Start is called before the first frame update
     void Start()
     {
         _controller = GetComponent<CharacterController>();
         _strafeSpeedMultiplier = 1.0f;
     }
 
     // Update is called once per frame
     void Update()
     {
 
 
         //if player is on the ground
         if (_controller.isGrounded)
         {
 
             //first, apply gravity
             _verticalVelocity = -_gravity * Time.deltaTime;
 
             //when player land on ground, his double jump ability is false 
             _canDoubleJump = false;
 
             //reset _strafeSpeedMultiplier if _canStrafe
             if (!_canStrafeLeft || !_canStrafeRight)
             {
                 _strafeSpeedMultiplier = 1.0f;
             }
 
 
             //activate strafe left by first releasing A
             if (Input.GetKeyUp(KeyCode.A))
             {
                 //on pressing A again within timeframe of IEnumerator (StrafeReset), you can strafe on left
                 _canStrafeLeft = true;
                 StartCoroutine(StrafeReset());
             }
 
             //activate strafe right by first releasing D
             if (Input.GetKeyUp(KeyCode.D))
             {
                 //on pressing D again within timeframe of IEnumerator (StrafeReset), you can strafe on right
                 _canStrafeRight = true;
                 StartCoroutine(StrafeReset());
             }
 
             //strafe left double pressing A
             if (Input.GetKeyDown(KeyCode.A) && _canStrafeLeft)
             {
                 Strafe();
                 //strafe on left side
                 _canStrafeLeft = false;
             }
 
             //strafe right double pressing D
             if (Input.GetKeyDown(KeyCode.D) && _canStrafeRight)
             {
                 Strafe();
                 //strafe on right side
                 _canStrafeRight = false;
             }
 
 
             //Jump
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 _verticalVelocity = _jumpForce;
                 //set double jump to true when the player has jump;
                 _canDoubleJump = true;                
             }
 
         }
         else
         //if not grounded
         {
             //apply gravity
             _verticalVelocity -= _gravity * Time.deltaTime;
 
             //apply double jump
             if (_canDoubleJump && Input.GetKeyDown(KeyCode.Space))
             {
                 _verticalVelocity = _jumpForce;
 
                 //you cannot triple jump...
                 _canDoubleJump = false;
             }
         }
 
 
         //MOVE THE PLAYER
         //update each axis for movement
         moveVector.x = Input.GetAxis("Horizontal") * _speed * _strafeSpeedMultiplier;
         moveVector.z = Input.GetAxis("Vertical") * _speed;
         //apply gravity on y axis
         moveVector.y = _verticalVelocity;
 
 
         //rotate for local space of the camera
         moveVector = transform.transform.TransformDirection(moveVector);
 
 
         //finally, move the player
         _controller.Move(moveVector * Time.deltaTime);
         
     }
 
     //reset the possibility to strafe if it's too long between the double press (A & D). timeframe of 0.08sec
     IEnumerator StrafeReset()
     {
         yield return new WaitForSeconds(0.08f);
         _canStrafeLeft = false;
         _canStrafeRight = false;
     }
 
 
 
     //strafing method
     private void Strafe()
     {
         //make a little jump
         _verticalVelocity = _strafeJump;
         //increase speed for dashing effect
         _strafeSpeedMultiplier = 6.0f;
 
         
     }
 
 }
 
 
 
 
 
 










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

181 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

Related Questions

Using "AddForce" with a Character Controller 1 Answer

AddForce and Momentum‏ 1 Answer

need help badly 2 Answers

AddForce in Character Controller problem 0 Answers

How to create smooth jump with rigidbody.addforce? 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