Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

W A S D Key Trouble *FIXED*

Discussion in 'Scripting' started by StracurioProductions, Dec 11, 2016.

  1. StracurioProductions

    StracurioProductions

    Joined:
    Dec 11, 2016
    Posts:
    19
    Hi everyone! I'm trying to create a new FPS game, but I am having trouble with the W, A, S, D keys. My W key is going left, my S key is going right, my A key is going backwards, and my D key is going forwards. I have included my code. Please help me out.
     

    Attached Files:

    • code.PNG
      code.PNG
      File size:
      19.5 KB
      Views:
      797
  2. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    Last edited: Dec 11, 2016
  3. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    That could be it, but I also spotted that he's doing simpleMove and that's the incorrect way of doing things, it will work if you keep your camera pointed at the original position but once you try to press your keys they will not adapt to which direction you're facing.

    Have you checked out Quill18's FPS tutorial? He does a very good explanation of this problem, I believe in part 3 he teaches you how to properly calculate movement without running into these issues.



    He explains what is wrong with simpleMove at 1:38 or so, just follow along with this series and you'll be able to fix it, though the code your working with looks a lot like the part 1 to begin with.
     
  4. StracurioProductions

    StracurioProductions

    Joined:
    Dec 11, 2016
    Posts:
    19
    Oh alright. Thank you! I'm actually following his tutorials so I guess that's a coincidence.
     
  5. StracurioProductions

    StracurioProductions

    Joined:
    Dec 11, 2016
    Posts:
    19
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class FirstPersonController : MonoBehaviour {
    public float movementSpeed = 5.0f;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
    float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
    Vector3 speed = new Vector3( sideSpeed, 0, forwardSpeed );

    CharacterController cc = GetComponent<CharacterController>();
    cc.SimpleMove(speed);
    }
    }
     
  6. StracurioProductions

    StracurioProductions

    Joined:
    Dec 11, 2016
    Posts:
    19
    I have fixed my problems.