Dodge effect / Left and Right movement.

So I’m trying to get my player character to get a dodge effect when the player double taps “A” or “D” to avoid enemy fire. I have the double tap and everything programmed in, and it works when it happens, but the effect is lacking when the player is on the ground. When the player double taps in the air, I have my desired effect ( A quick burst of speed to the left or right ). It seems that the ground stops this from happening. I have tried to put a zero friction surface under him, but that didn’t work. I will let you know, that this game is based on a rail/spline which is constantly moving the player along no matter what. Let me know if anyone has any suggestions to get the player to do this quick left/right motion while on the ground. I would appreciate it.

Here is my code so far…

using UnityEngine;
using System.Collections;

public class PlayerDodge : MonoBehaviour {

public Rigidbody rb;
public float dodgeSpeed;
public Vector3 dodge = new Vector3 (5, 5, 5);
public float buttonCoolDown = 0.5f;
public float lastTapTime = 0;

void Start ()
{
GameObject player = GameObject.Find(“ThirdPersonController”);
rb = player.GetComponent();

lastTapTime = 0;

}

void Update ()
{

if (Input.GetKeyDown(“d”))
{
if((Time.time - lastTapTime) < buttonCoolDown)
{
DodgeRight();
}

lastTapTime = Time.time;
}

if (Input.GetKeyDown(“a”))
{
if((Time.time - lastTapTime) < buttonCoolDown)
{
DodgeLeft();
}

lastTapTime = Time.time;
}

}

void DodgeRight()
{
rb.AddForce(dodge * dodgeSpeed );
}

void DodgeLeft ()
{
rb.AddForce(-dodge * dodgeSpeed );
}

}

I have noticed that as well, that the player may be colliding with the ground. My question to that is, can I avoid the ground by using the code I’m currently using? Or is there a better way to implement instead of using AddForce?

Well, you are adding a Force to make the object dodge, so I guess that :

  • Your player is colliding with the ground

or

  • Your player’s drag is too high