I have a simple problem which is while player is attacking i want move function to stop(that i accomplish); after that move function doesn’t work. here is my code. if anyone can point me to right direction, it’d be so good for me. thank you.
public class Controller : MonoBehaviour {
public float moveSpeed, turnSpeed;
public float height = 0.5f, heightPadding = 0.05f, maxGroundAngle = 120;
float angle, moveAmount, actionDelay;
float groundAngle;
public LayerMask ground;
public bool debug;
bool grounded;
[HideInInspector]
public bool input_LM, input_RM, input_Space, input_LShift, input_E, input_Q, input_F;
public bool inCombat, equipped, lockedOn, canMove, inAction;
Vector3 forward;
RaycastHit hitInfo;
Vector2 input;
Quaternion targetRotation;
Transform cam;
Rigidbody rigid;
Animator anim;
void Start(){
canMove = true;
cam = Camera.main.transform;
rigid = GetComponent<Rigidbody>();
rigid.angularDrag = 999;
rigid.drag = 4;
anim = GetComponentInChildren<Animator>();
}
void Update(){
GetInput();
HandleAnimations();
if (inAction)
canMove = false;
else
canMove = true;
CalculateDirection();
CalculateForward();
CalculateGroundAngle();
CheckGround();
if (Mathf.Abs(input.x) < 1 && Mathf.Abs(input.y) < 1)
return;
Rotate();
if(canMove == true)
Move();
if (Input.GetButton("Interact"))
Interact();
}
void LateUpdate(){
}
void GetInput(){
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
input_LM = Input.GetButtonUp("Left Mouse");
input_RM = Input.GetButtonUp("Right Mouse");
input_LShift = Input.GetButtonUp("Dash");
input_Space = Input.GetButtonUp("Roll");
input_Q = Input.GetButtonUp("Cast Left");
input_E = Input.GetButtonUp("Cast Right");
input_F = Input.GetButtonUp("Interact");
}
void CalculateDirection(){
angle = Mathf.Atan2(input.x, input.y);
angle = Mathf.Rad2Deg * angle;
angle += cam.eulerAngles.y;
}
void Rotate(){
targetRotation = Quaternion.Euler(0, angle, 0);
rigid.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
}
void Move(){
if (groundAngle >= maxGroundAngle)
return;
rigid.MovePosition(transform.position + forward * moveSpeed * Time.deltaTime);
}
void CalculateForward(){
if (!grounded){
forward = transform.forward;
return;
}
forward = Vector3.Cross(transform.right, hitInfo.normal);
}
void CalculateGroundAngle(){
if (!grounded){
groundAngle = 90;
return;
}
groundAngle = Vector3.Angle(hitInfo.normal, transform.forward);
}
void CheckGround(){
if (Physics.Raycast(transform.position, -Vector3.up, out hitInfo, height + heightPadding, ground)){
grounded = true;
}else{
grounded = false;
}
}
void DrawDebugLines(){
if (!debug)
return;
Debug.DrawLine(transform.position, transform.position + forward * height, Color.blue);
Debug.DrawLine(transform.position, transform.position - Vector3.up * height, Color.green);
}
void Interact(){
}
public void CheckForEquip(){
if (!inCombat){
equipped = false;
lockedOn = false;
}else{
equipped = true;
}
}
void HandleAnimations(){
CheckForEquip();
//movement
anim.SetFloat("Horizontal", Mathf.Abs(input.x));
anim.SetFloat("Vertical", Mathf.Abs(input.y));
//actions
if (input_LM == false && input_RM == false && input_E == false
&& input_Q == false && input_F == false && input_Space == false && input_LShift == false){
return;
}
string targetAnimation = null;
if (input_LM){
inAction = true;
targetAnimation = "Attack_05";
}
if (input_RM){
inAction = true;
targetAnimation = "Attack_01";
}
anim.CrossFade(targetAnimation, 0.2f);
rigid.velocity = Vector3.zero;
if (inAction){
anim.applyRootMotion = true;
Vector3 delta = anim.deltaPosition;
delta.y = 0;
Vector3 v = (delta * 1) / Time.deltaTime;
rigid.velocity = v;
rigid.drag = 0;
}
anim.applyRootMotion = false;
}
}