Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Scarvion · May 23, 2014 at 10:31 AM · characterjoystickmovedirection

direction of joystick to control my character movements

im using NavMesh to control my character movements i set a target which the character will follow but now i only can move in the horizontal and vertical axis but not the diagonal axis here is my javascript i currently have;

 private var joystickObj : GameObject;
 private var joystick : Joystick; 
 var target : Transform;// Set target forn player to move to
 var agent : NavMeshAgent;
 
 function Start() {
     joystickObj = GameObject.Find("Single Joystick");
     joystick = joystickObj.GetComponent(Joystick);
     agent = GetComponent(NavMeshAgent);
 }
 
 function Update() {
 
 
 if (joystick.position.x > .5) {
  
        target.position = Vector3(transform.position.x + 2,transform.position.y,transform.position.z);
        Debug.Log("moveright");
     }
     else if (joystick.position.x < -.5) {
     target.position = Vector3(transform.position.x - 2,transform.position.y,transform.position.z);
     Debug.Log("moveleft");
         
     }
     else if (joystick.position.y > .5) {
     target.position = Vector3(transform.position.x,transform.position.y,transform.position.z + 2);
     Debug.Log("moveup");
         
     }
     else if (joystick.position.y < -.5) {
     target.position = Vector3(transform.position.x,transform.position.y ,transform.position.z -2);
     Debug.Log("movedown");
         
     }
 
 
 agent.SetDestination(target.position);
 
 }
Comment
Add comment · Show 2
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 Benproductions1 · May 23, 2014 at 12:15 PM 0
Share

Please format your code, if you don't know how, watch the tutorial video on the right.

Also, please refrain from using excessively long question titles. Something more concise will get you many more views on your question, increasing the chance someone will comment or answer.

avatar image Scarvion · May 23, 2014 at 05:12 PM 0
Share

Thanks for the tip ben first time using unityAnswer :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · May 23, 2014 at 08:36 PM

Of course you can only move in one direction. Look at your own code. Its 4 mutually exclusive ifs, one for each direction.

If you want to be able to read more then one at once remove the "else" from the start of each if clause.

Comment
Add comment · Show 1 · 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 Scarvion · May 24, 2014 at 04:22 PM 0
Share

as in i wanted the target of my player to be 2 units infront of my player and also at the direction of the joystick, removing the else did help but it still gave me only 4 directions of movement

avatar image
0

Answer by abhishek7 · May 24, 2014 at 04:30 PM

All you need to do is remove the else ifs and replace them with just ifs. This way the program will take into account all possible movements in one update and will move as you want. THEN add if x and y is both moved move it so that x is moved up one and y is moved up one to move diagonally; add it for every possibility, enabling diagonal control.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Basic Character Transform with Joystick 1 Answer

iOS Rotation Chararacter with joystick 1 Answer

virtual joystick not working in mobile 1 Answer

Ball Character controlled by a single Joystick? 0 Answers

How can I use a joystick for a mobile app to control specific limbs of a character? 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