Help! Player won't stop moving when I release the W key,Help! Player won't stop walking when I release the W key
So when I release the W key for a short second, it stops walking. But after a couple times it just continues walking and won't stop. I'm following a Youtube tutorial and I don't know where I'm going wrong.
Link to video: https://www.youtube.com/watch?v=ReauId6jFFI
My code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class KnightController2 : MonoBehaviour {
float speed = 4.0f;
float rotSpeed = 80.0f;
float rot = 0f;
float gravity = 8.0f;
Vector3 moveDir = Vector3.zero;
CharacterController controller;
Animator anim;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (controller.isGrounded)
{
if (Input.GetKey(KeyCode.W))
{
anim.SetInteger("condition", 1);
moveDir = new Vector3(0, 0, 1);
moveDir = moveDir * speed;
moveDir = transform.TransformDirection(moveDir);
}
if(Input.GetKeyUp (KeyCode.W))
{
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);
}
} ,So when I release the W key for a short time the player will stop walking, but after a few times it continues walking and won't stop. Not sure what I'm doing wrong. I followed a Youtube tutorial and code seems to match..
Video I'm following: https://www.youtube.com/watch?v=ReauId6jFFI
public class KnightController2 : MonoBehaviour {
float speed = 4.0f;
float rotSpeed = 80.0f;
float rot = 0f;
float gravity = 8.0f;
Vector3 moveDir = Vector3.zero;
CharacterController controller;
Animator anim;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (controller.isGrounded)
{
if (Input.GetKey(KeyCode.W))
{
anim.SetInteger("condition", 1);
moveDir = new Vector3(0, 0, 1);
moveDir = moveDir * speed;
moveDir = transform.TransformDirection(moveDir);
}
if(Input.GetKeyUp (KeyCode.W))
{
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);
}
}
Your answer
Follow this Question
Related Questions
Question on Unity5 CharacterController performance/efficiency 0 Answers
I can't rotate and move in the opposite direction with character Controller 0 Answers
Problem with character controller movement and gravity 0 Answers
Pushed Crate/Cube won't drop while pushing into the void 0 Answers
CharacterController glitches if it reaches a ledge while jumping into a wall. 4 Answers