• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by brianchan661 · Sep 22, 2013 at 10:11 AM · movementcharacterdirection

about character movement in 8 direction

i am write a javascript in order to control the movement of my character, but i have problem in the movement in the remaining 4 direction, w$$anonymous$$ch are top right left and down right left here are part of my code

 if(Input.GetKey("w")) {
         animation.CrossFade("moving"); //animation
         direction = Vector3.forward;  
           transform.forward = new Vector3(0f, 0f, 1f);
         t$$anonymous$$s.transform.position += t$$anonymous$$s.transform.TransformDirection(new Vector3(0, 0, speed));     //turning the character facing direction and move
 }
 else if(Input.GetKey("s")){

where the remain code is for the down right and left

my problem is that if i continue to use else if (Input.GetKey("w")&&Input.GetKey("d")) it will be useless as it already go into the Input.GetKey("w") or ("d") how should i do?

further more, i would aslo like to ask how can i check for a double click?say click "w" 2 times.

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by robertbu · Sep 22, 2013 at 10:34 AM

In order to get 8 directions of movement, you can combine some vectors. Start with a new scene and cube and add t$$anonymous$$s script to the cube:

 #pragma strict
 
 var speed = 2.0;
  
 function Update () {
     var direction = Vector3.zero;
     if(Input.GetKey(KeyCode.A)) {
        direction += Vector3.left;  
     }
     if(Input.GetKey(KeyCode.S)) {
        direction += Vector3.back;  
     }
     if(Input.GetKey(KeyCode.D)) {
        direction += Vector3.right;  
     }
     if(Input.GetKey(KeyCode.W)) {
        direction += Vector3.forward;  
     }
     
     transform.Translate(direction * speed * Time.deltaTime);
 }

Alternately you can use GetAxisRaw() to build the vector. "Horizontal" and "Vertical" are by default mapped to the ADWS keys (as well as the arrow keys).

 #pragma strict
 
 var speed = 2.0;
 
 function Update() {
     var direction = Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
     transform.Translate(direction * speed * Time.deltaTime);
 }
Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image brianchan661 · Sep 22, 2013 at 01:17 PM 0
Share

great!Thank you for your help! then how about checking double click of a key? say pressing ww than my charcter will run

avatar image robertbu · Sep 22, 2013 at 03:19 PM 0
Share

Note for the future, please keep questions to a single issue. As for double click, you can use a timestamp. At the top of the file:

 var doubleClickTime = 0.15;
 private var timestamp = -1.0;

And then in Update():

 if (Input.GetKeyDown(KeyCode.W)) {
   if (Time.time - timeStamp < doubleClickTime) {
     Debug.Log("Double click");
   }
   timestamp = Time.time;
 }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Rotate the Player In Direction of Movement? 4 Answers

Help with my Character Controller 1 Answer

Making a character move automatically in one direction. 2 Answers

What movment does the character do 1 Answer

Camera relative movement 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges