I am trying to make my character be able to evade attacks. My movement script is quite long but this is the portion of code I am using.
if(evading)
{
moveDirection = playerRotation.transform.forward / 5 * evadeSpeed;
moveSpeed = evadeSpeed;
}
The problem here is that I don’t have control over the two things I need:
The distance of the roll and the speed in which the character covers the distance. I am uncertain how to control these.
@HappyMoo’s comment : The evasion occurs in the forward direction because the forward direction is the direction the character is facing, that’s the style I want for my game.
void ProcessEvasionTime()
{
//evasion timer for action performed
if(evading)
{
evadeTimer -= Time.deltaTime;
if(evadeTimer <= 0)
{
anim.SetBool("Evading", false);
evading = false;
cooldownInEffect = true;
}
}else
{
evadeTimer = evadeTime;
}
if(!evading && InputManager.ActiveDevice.GetControl(InputControlType.Action1))
{
ProcessEvasion();
}
if(cooldownInEffect)
{
cooldownTimer -= Time.deltaTime;
if(cooldownTimer <= 0)
{
cooldownInEffect = false;
cooldownTimer = cooldown;
}
}
}
void ProcessEvasion()
{
if(!cooldownInEffect)
{
//timers
evading = true;
evadeTimer = evadeTime;
//move character to evade and set animator to play evasion animation
anim.SetBool("Evading", true);
}
}
Character Movement:
Note - The input system is a custom script to allow cross controller support, just think of it as input.getaxis or relevant alternative.
void Update()
{
InputManager.Update();
//cache the inputs
//h = Input.GetAxis ("Horizontal");
h = InputManager.ActiveDevice.GetControl(InputControlType.LeftStickX);
//v = Input.GetAxis ("Vertical");
v = InputManager.ActiveDevice.GetControl(InputControlType.LeftStickY);
//s = Input.GetAxis ("Strafe");
//mh = Input.GetAxis ("Mouse X");
//zero the move direction so previous data is removed
moveDirection = Vector3.zero;
//check if it's grounded, otherwise player cannot move, also check for the deadzone
if(characterController.isGrounded)
{
if (v > deadZone || v < -deadZone)
{
moveDirection = new Vector3 (0f, 0f, v);
}
//add horizontal movement to the (currently)vertical movement vector
moveDirection.x = h;
//process turning
//ProcessTurning(s, mh);
//normalize the diagonal inputs to prevent values above 1
if (moveDirection.magnitude > 1)
{
moveDirection.Normalize ();
}
//move character in the direction of the camera's facing angle
moveDirection = cam.transform.TransformDirection(moveDirection);
}
//change movement speed if weapon unsheathed
if(anim.GetBool("Unsheathed") == true && !evading)
moveSpeed = walkSpeed;
else
moveSpeed = runSpeed;
//process evasion timers
if(combo!= null && combo.attackTimer <= 0)
ProcessEvasionTime();
//all events for when the player loses control of character eg. talking, cutscene, death, evading
RevokeControl();
//set the animator's Speed float to the magnitude of the moveDirection to allow for the run animation to play
anim.SetFloat ("Speed", moveDirection.magnitude);
//calculate gravity
moveDirection.y = -gravity * Time.deltaTime;
//move the character
characterController.Move (moveDirection * moveSpeed * Time.deltaTime);
transform.Rotate(turnDirection * Time.deltaTime);
//control player facing direction when strafing, moving backward, etc
if(moveDirection.x > deadZone || moveDirection.x < -deadZone || moveDirection.z > deadZone || moveDirection.z < -deadZone)
{
desiredRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
playerRotation.transform.rotation = Quaternion.RotateTowards(playerRotation.transform.rotation, desiredRotation, rotateSpeed * 10 * Time.deltaTime);
}
}