Question by
HugoreMastore · May 30, 2019 at 06:17 PM ·
movementcharacter controllermechanimdashdodge
Make a dash system using the Character Controller of Unity
Hello there ! I'm trying to make a Top Down Shooter game with a dodge/dash/roll system. But the problem is here, I'm using the Character Controller from Unity for move my character and I think this is the problem why my character is not able to go to another position after pressing the input. The character just stand on place even if I control the dash movement with the animation itself ( in this case, the animation make the player move but at the end he go back to his last position ). I hope my english was not that bad and you can understand my problem ^^'
CharacterController charController;
Camera cam;
Vector3 moveDirection, moveDirectionController;
Vector3 dodgeDir;
Animator anim;
public bool useController;
public float fixedJoyRotation;
Vector3 moveInput;
float turnAmount, forwardAmount;
//////////// Var pour pouvoir regarder vers la bonne Dir
float rayLenght = 100.0f;
Vector3 lookPos, lookAtDirection;
//////////// Var pour les déplacements
float h, v, hBis, vBis;
public float dashSpeed, dashDuration;
[SerializeField]
float velocity = 8.0f;
float speed;
bool isMoving = false, canRollAgain = true, canMove = true;
[SerializeField]
float desiredRotationSpeed = 3.0f;
float allowPlayerRotation = 0.1f;
void Update()
{
InputMagnitude();
LookAtAiming();
DodgeRoll();
ConvertMoveInput();
UpdateAnimator();
}
private void FixedUpdate()
{
AnimationControl();
}
void InputMagnitude()
{
if (canMove == true)
{
//Calculate Input Vectors
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
//Calculate the Input Magnitude
speed = new Vector2(h, v).sqrMagnitude;
//Physically move player
if (speed > allowPlayerRotation)
{
isMoving = true;
//anim.SetFloat ("InputMagnitude", Speed, StartAnimTime, Time.deltaTime);
PlayerMoveAndRotation();
}
else if (speed < allowPlayerRotation)
{
isMoving = false;
//anim.SetFloat ("InputMagnitude", Speed, StopAnimTime, Time.deltaTime);
}
}
}
void PlayerMoveAndRotation()
{
Debug.DrawRay(transform.position, transform.forward * 10, Color.green);
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
//var camera = Camera.main;
var forward = cam.transform.forward;
var right = cam.transform.right;
forward.y = 0f;
right.y = 0f;
forward.Normalize();
right.Normalize();
// addition des 2 v3 pour obtnir un v3 = la dir vers laquelle on se deplace
moveDirection = forward * v + right * h;
//transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirection), desiredRotationSpeed);
// On clamp aussi sa vitesse pour l'empecher de se déplacer plus rapidement sur le côté
charController.Move(Vector3.ClampMagnitude(moveDirection, 1.0f) * Time.deltaTime * velocity);
}
void LookAtAiming()
{
if (canMove == true)
{
if (useController)
{
lookAtDirection = Vector3.right * Input.GetAxisRaw("RHorizontal") + Vector3.forward * -Input.GetAxisRaw("RVertical");
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
var camera = Camera.main;
var forward = camera.transform.forward;
var right = camera.transform.right;
forward.y = 0f;
right.y = 0f;
forward.Normalize();
right.Normalize();
Vector3 lookDir = moveDirection - transform.position;
// on ne veut pas regarder en hauteur
lookDir.y = 0;
moveDirection = (forward * v + right * h);
// Quand on touche au joystick Droit
if (lookAtDirection.sqrMagnitude > 0.00115f)
{
transform.rotation = Quaternion.LookRotation(lookAtDirection, Vector3.up) * Quaternion.Euler(0, fixedJoyRotation, 0);
//Debug.Log("sqr = " + playerDirection.sqrMagnitude);
}
// Quand on NE touche PAS au joystick Droit
if (lookAtDirection.sqrMagnitude <= 0.00115f && moveDirection.sqrMagnitude > 0.001f)
{
// Il regarde dans la direction vers laquelle il se deplace
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirection), desiredRotationSpeed);
}
}
}
}
void DodgeRoll()
{
h = Input.GetAxisRaw("Horizontal");
v = Input.GetAxisRaw("Vertical");
var camera = Camera.main;
var forward = cam.transform.forward;
var right = cam.transform.right;
forward.y = 0f;
right.y = 0f;
forward.Normalize();
right.Normalize();
dodgeDir = h * right + v * forward;
if (Input.GetButtonDown("Fire3") && isMoving == true && canRollAgain == true)
{
//charController.Move(Vector3.Lerp(transform.position, Vector3.forward * dashSpeed, 1));
//transform.position = Vector3.Lerp(transform.position, Vector3.forward * dashSpeed, 0.5f);
charController.enabled = false;
StartCoroutine("MoveToPosition");
canMove = false;
canRollAgain = false;
// lance lanim
//anim.applyRootMotion(true);
anim.SetTrigger("Dodge");
}
}
public IEnumerator MoveToPosition(Transform transform, Vector3 position, float timeToMove)
{
Debug.Log("Dodge");
Vector3 currentPos = transform.position;
float t = 0f;
while (t < 1)
{
t += Time.deltaTime / timeToMove;
transform.position = Vector3.Lerp(currentPos, Vector3.forward * dashSpeed, t);
yield return null;
}
}
Comment