- Home /
Character Wont Stop Moving
I'm scripting movement for a 3D model with an animation, and I thought I scripted it at line 33-36 to STOP moving after you let go of the 'W' key. It isn't working. If anyone can help, that would be nice. Thank you.
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading;
using UnityEngine;
public class KniteController : MonoBehaviour
{
float speed = 4;
float rotSpeed = 80;
float gravity = 8;
Vector3 moveDir = Vector3.zero;
CharacterController controller;
Animator anim;
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
void Update()
{
if(controller.isGrounded)
{
if(Input.GetKey(KeyCode.W))
{
moveDir = new Vector3 (0, 0, 1);
moveDir *= speed;
}
if (Input.GetKeyUp(KeyCode.W))
{
moveDir = new Vector3 (0, 0, 0);
}
}
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
}
try else if
instead of if. Im quite stumped actually. This looks perfect, but it somehow doesnt work?
$$anonymous$$y only guess is that somehow controller.isGrounded is set to false after you start moving, you might wanna check that
Answer by Artik2442 · Jul 30, 2020 at 11:20 AM
Maybe you can use GetKeyDown and not GetKey at line 28...
In your script you can set the Move Direction only one time and it will work like a simple GetKey. (I think...)
There is an other way:
Replace the line 33 by an else statement. And the script will do:
GetKey -> true -> moveDir = new Vector3 ... GetKey -> false -> moveDir = Vector3.zero;
Your answer
Follow this Question
Related Questions
Unity Build Showing Black Screen on Startup Using Oculus SDK 0 Answers
Dice Value 1 Answer