- Home /
W key to move doesnt work,Movement Script doesnt do anything when i press w
Ill just get straight to the point, when i press the w key my character doesnt move or play an animation, heres me code:
If there's a similar script in unity assets please let me know how to get to it :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class walkcondition : MonoBehaviour
{
float speed = 4;
float rotSpeed = 80;
float gravity = 8;
Vector3 moveDir = Vector3.zero;
CharacterController controller;
Animator anim;
// Use this for initialization
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (controller.isGrounded)
{
NewMethod();
}
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
private void NewMethod()
{
if (Input.GetKey(KeyCode.W))
{
moveDir = new Vector3(0, 0, 1);
moveDir *= speed;
}
}
}
Hi, did you step through the code execution? It might be that controller.isGrounded
is never true, so the New$$anonymous$$ethod() will never be called in the first place
I can't see a lot wrong with your code. As $$anonymous$$larzahs points out, try debugging your code to see if it works the way you expect and if the CharacterController is even grounded. If that's the case, you could try swapping the $$anonymous$$eyCode.W with "w", but I don't think that will make a difference. One last thing, when you are creating floats place an f behind the number like so : 8.0f. If you don't place the f it automatically creates a double, maybe there's a problem with that and controller.$$anonymous$$ove doesn't like doubles.