- Home /
Moving a 2D Object a certain distance over time with a single button click
Title kind of says it all, I've been trying to figure out how to make my 2D character move a specified distance on a single button click, not like a teleport, but like a dash or a dodge
so for example, If i pressed the z button, I'd want them to move 500 ft over the course of 2 seconds or something
I lost the code I was trying out due to some weird saving problem but I think I was honestly over thinking the problem anyway and writing overly complex code that didnt even work
I feel like there is a simple solution to this problem, but I havent been able to find it
If anyone knows something, I'd appreciate it!
Thanks in advance!
Answer by megamika2 · Apr 05, 2018 at 08:11 PM
Hello i,d like to ask you in what way do you move your character. By using transform.Translate or by using your rigidbody?
Hello!
for the standard movement I'm adding force to the rigidbody using Input.GetAxis
You can try this.
bool DogingIsHappening;
public float DogeTime;
private void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Z) & DogingIsHappening == false)
{
StartCoroutine(Doge());
}
if (DogingIsHappening)
{
//$$anonymous$$ove to a direction
}
}
IEnumerator Doge()
{
DogingIsHappening = true;
yield return new WaitForSeconds(DogeTime);
DogingIsHappening = false;
}
If you have any results or questions reply to me.