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 ZawartCorp · Feb 15, 2014 at 01:39 AM · 2dmovementjoystick

Help with joystick movement in 2d game

Greetings to everyone from already doi thanks for your attention.

I have a problem with the script I use to move my character with the single joystick assets of unity.

So far the precionar the right, left and side up but the character moves when the joystick down precionar character goes well.

I leave the code that I have so far

 public var velocidad   : Vector2  = new Vector2(50, 50);         
 private var movimiento : Vector2;
 private var thisTransform : Transform;    
 private var velocity : Vector2;    
 var moveJoystick : Joystick;
     
 function Start (){
     thisTransform = GetComponent( Transform );
 }
 function Update () {    
 
 var movement : Vector2 = thisTransform.TransformDirection( Vector2( moveJoystick.position.x, moveJoystick.position.y ) );
 var absJoyPos : Vector2 = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
             
 if ( moveJoystick.position.y > 0 ){
         
 movimiento = new Vector2(velocidad.x * absJoyPos.x, velocidad.y * absJoyPos.y);                
 }else{
 movimiento = new Vector2(velocidad.x * absJoyPos.x, velocidad.y * absJoyPos.y);
 }
             
 if ( moveJoystick.position.x > 0){
 movimiento = new Vector2(velocidad.x * absJoyPos.x, velocidad.y * absJoyPos.y);    
 }else{
 movimiento = new Vector2(velocidad.x * absJoyPos.x * -1, velocidad.y * absJoyPos.y);                
 }                        
 }
 function FixedUpdate(){
 
 rigidbody2D.velocity = movimiento;
 
 
 }

The game is 2D and I just need to move in the x and y axes, which as I said you get what but when you press the joystick down the character goes.

I have days trying to fix this and I have searched many places without finding the solution.

Thank you very much and sorry for my bad English

Comment
Add comment · Show 1
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 hammad-zahid · Dec 15, 2015 at 03:08 PM 0
Share

Did you get your solution? if it is please provide your movement code. i want directional movement... It is appreciated...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by wibble82 · Dec 15, 2015 at 03:36 PM

I'm afraid I don't fully understand your question, but if all you are trying to do is set the rigid body's velocity based on the joy stick, why not:

 function Update () {    
  
     //get the velocity (you might even just be able to do newVelocity = moveJoystick.position
     //but I don't know about moveJoystick so am not sure)
     var newVelocity : Vector2 = Vector2(moveJoystick.position.x, moveJoystick.position.y);
 
     //maybe we multiply it by something if we want the player to be faster or slower
     newVelocity *= 5.0f;
  
     //now store it in the rigid body
     rigidbody2D.velocity = newVelocity;
  }

Just so you're aware, the most obvious bug in your code is on line 19 - for your code to work at all you would need to add a '-1' to the y, just like you add a -1 to the x in the other section. However I think you've made it a little more complex than it needs to be, and there's several other reasons you will have problems with it, so I'd go with my example above!

-Chris

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

Answer by LordDarkon76 · Dec 15, 2015 at 10:01 PM

Or just remove the absJoyPos and use the raw input to move.

Also don't mix languages (spanish english) it make your code harder to read.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Joystick problems 1 Answer

Moving 2D Sprite Player 0 Answers

Joystick 2D Sprite Rotation and Movement 0 Answers

Introducing joystick to Unity? 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