Move Foward key W sometimes keeps walking after I let go of W, only happens afew times
Hi Guys,
I am fairly new to Unity, first time posting here.
I have an issue with my W to walk forward. There are usually 3 instances where I will press W, then let go and it will keep moving forward. It happens the very first time I press W, and then after I press W again it will go back to my idle/standing animation. After this first issue, it then works normally where it walks when I press W, and stops when I let go of W. It will then proceed to happen 1 or 2 more times at random after some more successful walking attempts.
I have pasted in my full script. I have been following this guide: https://www.youtube.com/watch?v=ReauId6jFFI
Thanks in advance :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
float speed = 4;
float rotSpeed = 80;
float rot = 0f;
float gravity = 8;
Vector3 moveDir = Vector3.zero;
CharacterController controller;
Animator anim;
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
void Update()
{
Movement();
GetInput();
}
void Movement()
{
if (controller.isGrounded)
{
if (Input.GetKey(KeyCode.W))
{
if (anim.GetBool("attacking") == true)
{
return;
}
else if (anim.GetBool("attacking") == false)
{
anim.SetBool("walk", true);
anim.SetInteger("condition", 1);
moveDir = new Vector3(0, 0, 1);
moveDir *= speed;
moveDir = transform.TransformDirection(moveDir);
}
}
if (Input.GetKeyUp(KeyCode.W))
{
anim.SetBool("walk", false);
anim.SetInteger("condition", 0);
moveDir = new Vector3(0, 0, 0);
}
}
rot += Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, rot, 0);
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
void GetInput()
{
if (controller.isGrounded)
{
if (Input.GetMouseButtonDown(0))
{
if (anim.GetBool("walk") == true)
{
anim.SetBool("walk", false);
anim.SetInteger("condition", 0);
}
if (anim.GetBool("walk") == false)
{
Attacking();
}
}
}
}
void Attacking()
{
StartCoroutine(AttackRoutine());
}
IEnumerator AttackRoutine()
{
anim.SetBool("attacking", true);
anim.SetInteger("condition", 2);
yield return new WaitForSeconds(0.800F);
anim.SetInteger("condition", 0);
anim.SetBool("attacking", false);
}
}
Answer by xonin · Aug 04, 2019 at 10:00 AM
If anyone needs to know, I figured out that it is because the terrain my player is walking on is not completely flat. if (controller.isGrounded) does not like that at all. I tested this as on a flat plane my character movement works without an issue. If anyone has any advice on how to deal with this issue I would really appreciate it. Thank you.
I'd suggest moving
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.W)){
// ...
}
out of
if (controller.isGrounded){
// ...
}
Hi there, at first I misunderstood your reply but I realise now that it looks like it should work! Thanks for your reply, appreciate it! I will let you know how it goes when I test it out tonight :)
Hello again, just want to say THAN$$anonymous$$S SO $$anonymous$$UCH! I just adjusted it as you suggested and it works a charm!