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 /
avatar image
0
Question by Pringlestache · Mar 12, 2019 at 02:18 PM · scripting problemvector3charactercontrollermove an objectdash

Move CharacterController a specified distance away from starting position.

Hi!

I'm trying to create a Dash-move for my character using a Character Controller.

So I want to move him quickly a specified distance (4 meters) in the forward vector when I press a button, but I also want him to take slopes into account. By that I mean that I want him to slide up the slope until he's 4 meters away from his starting position (you can visualise this by having a sphere with a radius of 4 meters on the character, and the extents of that sphere is how far I want the character to be able to go in any direction). I figured using a Vector's magnitude would be a way to do it, but haven't been able to figure out exactly how.

I've included some of the code below. I've tried a bunch of things, and the problem with the version below is that he'll go longer/shorter than the dashDistance sometimes because the magnitude is in world space. I tried a bit with local vectors on the character, but I'm pretty new to programming so I haven't been able to make it work either.

Do any of you guys have any suggestions to how I could implement a dash this way?

 float dashDistance = 4.0f;

 IEnumerator ControllerDash()
 {
     //Get the starting positions, the target position and the offset
     startPosition = transform.position;
     targetPosition = startPosition + transform.TransformDirection(Vector3.forward * dashDistance);
     offset = targetPosition - transform.position;

     //Move the character while the offset magnitude is between 0.2 and 4.2.
     while (offset.magnitude > 0.2f && offset.magnitude < dashDistance + 0.2f)
     {
         print("We're moving towards the target");

         offset = targetPosition - transform.position;
         offsetMagnitude = offset.magnitude;
  
         characterController.Move(transform.TransformDirection(Vector3.forward) * dashSpeed * Time.deltaTime);

         yield return null;
     }

     print("We've reached the target");
 }








Comment
Add comment · Show 5
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 RobAnthem · Mar 12, 2019 at 04:01 PM 0
Share

I wouldn't use a coroutine since it releases the control from your update loop, something like this you need to handle fully. In my opinion the best way to handle this (aside from actually using root motion animations) is to override the input section of the controller by creating a bool to let the controller know that it is not currently receiving input from user but from another script, in this case, your charge dash function. I would create a secondary input parameter that you use to get speed from to use for this case, so that when you stop checking for input the player will move forward anyway at the specified speed for the specified time or distance. Does this make sense? Sorry I could probably explain it better.

avatar image Pringlestache RobAnthem · Mar 12, 2019 at 05:17 PM 0
Share

Hey, thanks for the reply! I have a bool that disables all input, I just decided to not include it here to save space :) As of right now I have the dash functionality in my $$anonymous$$ovement script. Would you recommend splitting the dash up into its own script? I've tested multiple different approaches to the dash and I have gotten it to work, but not precisely like I want. What I'm having trouble with spesifically is the act of moving the character to the target destination (the target destination in my case being 4m forwards)

avatar image xxmariofer RobAnthem · Mar 12, 2019 at 08:11 PM 0
Share

a loop corroutine with a yield return null is basicaly the same as the update event, there is no difference, it gets called every frame. the only thing i am not sure if its before or after update events.

avatar image RobAnthem xxmariofer · Mar 13, 2019 at 03:34 AM 0
Share

That's fair, but ridiculous because it creates more garbage than simply doing

 void SomeFunction()
 {
     //Do stuff
 }
 void Update()
 {
     SomeFunction();
 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by xxmariofer · Mar 12, 2019 at 08:08 PM

try changing the while to this since Vector A - B isnt the distance but the direction.

     while(4 > Vector3.Distance(startPosition, transform.position))
     {
         characterController.Move(transform.TransformDirection(Vector3.forward) * dashSpeed * Time.deltaTime);
     }
Comment
Add comment · Show 1 · 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 Pringlestache · Mar 13, 2019 at 12:57 PM 0
Share

This was EXACTLY what I was looking for. Works like a charm. So simple too! Thank you so much, you've saved me a lot of time. You have a great day! :D

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

204 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

Related Questions

Inverted Char control 1 Answer

Controller Movement Problem 1 Answer

OnCollisionEnter Sine Wave to Character 0 Answers

Smoother dash with a Character Controller 2 Answers

3D Dash ability 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