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
0
Question by HugoreMastore · May 30, 2019 at 06:17 PM · movementcharacter controllermechanimdashdodge

Make a dash system using the Character Controller of Unity

Hello there ! I'm trying to make a Top Down Shooter game with a dodge/dash/roll system. But the problem is here, I'm using the Character Controller from Unity for move my character and I think this is the problem why my character is not able to go to another position after pressing the input. The character just stand on place even if I control the dash movement with the animation itself ( in this case, the animation make the player move but at the end he go back to his last position ). I hope my english was not that bad and you can understand my problem ^^'

 CharacterController charController;
     Camera cam;
     Vector3 moveDirection, moveDirectionController;
     Vector3 dodgeDir;
     Animator anim;
     public bool useController;
     public float fixedJoyRotation;    
 
     Vector3 moveInput;
     float turnAmount, forwardAmount; 
 
     //////////// Var pour pouvoir regarder vers la bonne Dir
     float rayLenght = 100.0f;
     Vector3 lookPos, lookAtDirection;
 
     //////////// Var pour les déplacements
     float h, v, hBis, vBis;
     public float dashSpeed, dashDuration;
     [SerializeField]
     float velocity = 8.0f;
     float speed;
     bool isMoving = false, canRollAgain = true, canMove = true;
 
     [SerializeField]
     float desiredRotationSpeed = 3.0f;
     float allowPlayerRotation = 0.1f;
     
 void Update()
     {
         InputMagnitude();
         LookAtAiming();
         DodgeRoll();
         ConvertMoveInput();
         UpdateAnimator();        
     }
 
     private void FixedUpdate()
     {
         AnimationControl();
     }
 
     void InputMagnitude()
     {
         if (canMove == true)
         {
             //Calculate Input Vectors
             h = Input.GetAxis("Horizontal");
             v = Input.GetAxis("Vertical");
 
             //Calculate the Input Magnitude
             speed = new Vector2(h, v).sqrMagnitude;
 
             //Physically move player
             if (speed > allowPlayerRotation)
             {
                 isMoving = true;
                 //anim.SetFloat ("InputMagnitude", Speed, StartAnimTime, Time.deltaTime);
                 PlayerMoveAndRotation();
             }
             else if (speed < allowPlayerRotation)
             {
                 isMoving = false;
                 //anim.SetFloat ("InputMagnitude", Speed, StopAnimTime, Time.deltaTime);
             }
         }    
     }
 
     void PlayerMoveAndRotation()
     {
         Debug.DrawRay(transform.position, transform.forward * 10, Color.green);
 
         h = Input.GetAxis("Horizontal");
         v = Input.GetAxis("Vertical");    
 
         //var camera = Camera.main;
         var forward = cam.transform.forward;
         var right = cam.transform.right;
 
         forward.y = 0f;
         right.y = 0f;
 
         forward.Normalize();
         right.Normalize();
 
         // addition des 2 v3 pour obtnir un v3 = la dir vers laquelle on se deplace
         moveDirection = forward * v + right * h;    
 
         //transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirection), desiredRotationSpeed);
         // On clamp aussi sa vitesse pour l'empecher de se déplacer plus rapidement sur le côté
         charController.Move(Vector3.ClampMagnitude(moveDirection, 1.0f) * Time.deltaTime * velocity);
     }
 
     void LookAtAiming()
     {
         if (canMove == true)
         {
             if (useController)
             {
                 lookAtDirection = Vector3.right * Input.GetAxisRaw("RHorizontal") + Vector3.forward * -Input.GetAxisRaw("RVertical");
 
                 h = Input.GetAxis("Horizontal");
                 v = Input.GetAxis("Vertical");
 
                 var camera = Camera.main;
                 var forward = camera.transform.forward;
                 var right = camera.transform.right;
 
                 forward.y = 0f;
                 right.y = 0f;
 
                 forward.Normalize();
                 right.Normalize();
 
                 Vector3 lookDir = moveDirection - transform.position;
                 // on ne veut pas regarder en hauteur
                 lookDir.y = 0;
                 
                 moveDirection = (forward * v + right * h);
 
                 // Quand on touche au joystick Droit
                 if (lookAtDirection.sqrMagnitude > 0.00115f)
                 {
                     transform.rotation = Quaternion.LookRotation(lookAtDirection, Vector3.up) * Quaternion.Euler(0, fixedJoyRotation, 0);
                     //Debug.Log("sqr = " + playerDirection.sqrMagnitude);
                 }
 
                 // Quand on NE touche PAS au joystick Droit
                 if (lookAtDirection.sqrMagnitude <= 0.00115f && moveDirection.sqrMagnitude > 0.001f)
                 {
                     // Il regarde dans la direction vers laquelle il se deplace
                     transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirection), desiredRotationSpeed);
                 }
             }
         }
     }
     
 void DodgeRoll()
     {
         h = Input.GetAxisRaw("Horizontal");
         v = Input.GetAxisRaw("Vertical");
 
         var camera = Camera.main;
         var forward = cam.transform.forward;
         var right = cam.transform.right;
 
         forward.y = 0f;
         right.y = 0f;
 
         forward.Normalize();
         right.Normalize();    
 
         dodgeDir = h * right + v * forward;
 
         if (Input.GetButtonDown("Fire3") && isMoving == true && canRollAgain == true)
         {
             //charController.Move(Vector3.Lerp(transform.position, Vector3.forward * dashSpeed, 1));
             //transform.position = Vector3.Lerp(transform.position, Vector3.forward * dashSpeed, 0.5f);
             charController.enabled = false;
             StartCoroutine("MoveToPosition");
             canMove = false;
             canRollAgain = false;
             
             // lance lanim
             //anim.applyRootMotion(true);
             anim.SetTrigger("Dodge");
              }
     }
 
     public IEnumerator MoveToPosition(Transform transform, Vector3 position, float timeToMove)
     {
         Debug.Log("Dodge");
         Vector3 currentPos = transform.position;
         float t = 0f;
         while (t < 1)
         {
             t += Time.deltaTime / timeToMove;
             transform.position = Vector3.Lerp(currentPos, Vector3.forward * dashSpeed, t);
             yield return null;
         }
     }



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

231 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

Related Questions

How to do dodge Unity2D like Crossing Souls or Hyper Light Drifter. 0 Answers

Speed boost button for player 0 Answers

2.5D Platformer - Dashing towards the character is moving.,2.5D platformer - Dashing left and right while moving 0 Answers

First person movement with character controller does not detect ground properly 0 Answers

How can I make character accelerate and deccelerate using GetAxis? 0 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