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. Dismiss Notice

WASD Movement Script

Discussion in 'Scripting' started by iSleepzZz, Jan 20, 2016.

  1. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    201
    Hey guys! So in my update I have this so far:

    Code (CSharp):
    1. if (Input.GetKey(KeyCode.W))
    2.                 rigidBody.velocity = forward * speedOut * walkspeed + new Vector3(0, rigidBody.velocity.y, 0);
    3.             if (Input.GetKey(KeyCode.S))
    4.                 rigidBody.velocity = -forward * speedOut * walkspeed + new Vector3(0, rigidBody.velocity.y, 0);
    5.             if (Input.GetKey(KeyCode.A))
    6.                 rigidBody.velocity = -right * speedOut * walkspeed + new Vector3(0, rigidBody.velocity.y, 0);
    7.             if (Input.GetKey(KeyCode.D))
    8.                 rigidBody.velocity = right * speedOut * walkspeed + new Vector3(0, rigidBody.velocity.y, 0);
    And it works perfectly, however, I want it to move diagonal also when you do combination keys like W & A will move up-left. Or I want W&D to move up-right and etc.
    How can I do this?
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Add the velocity vectors?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    wasd is included in the default input axis...

    Code (csharp):
    1.  
    2. Vector3 inputVector = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).Normalize();
    3. rigidBody.velocity = (inputVector * speedOut * walkSpeed) + new Vector3(0, rigidBody.velocity.y, 0);
    4.  
     
  4. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    201
    Solved already, thanks.
     
  5. sushielolbtw_YT

    sushielolbtw_YT

    Joined:
    Jul 8, 2021
    Posts:
    1
    it says all complier errors must be fixed and that was a error
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    Notice this thread is five years old. Don't post to old threads, it's against forum rules.

    Notice that your post contains absolutely zero information ("a error..."). Don't do that, it's not helpful.

    When you have a problem, start your own post (it's FREE!) and keep this in mind.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    Remember: NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    Always start with the FIRST error in the list, as sometimes that error causes or compounds some or all of the subsequent errors.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/