- Home /
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");
}
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.
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)
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.
That's fair, but ridiculous because it creates more garbage than simply doing
void SomeFunction()
{
//Do stuff
}
void Update()
{
SomeFunction();
}
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);
}
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
Follow this Question
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