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 Shrall Pyro · Apr 17, 2014 at 12:47 PM · javascriptmobilemovement scriptjoystick

[mobile development] 2D Joystick Controller

hi everyone, first i'm sorry for mistake i can do with my english ! I've a problem with my code about mouvement for my little game. the movement i want is on X and Y axis. I've used the standart assest for mobile, the CameraRelativeController. but he doing so much things for my game, so I've edit the code. because my player can move on a 3D environment, and it's a 2D game, as a JetPack Joyroid movement.

The player can move correctly on X axis, but i don't know how to move on Y axis. I hope you can help me to solve this. I you need to know, this is the only code on Js, i'm scripting on C#

The new code on CameraRelativeController :

 #pragma strict
 
 // This script must be attached to a GameObject that has a CharacterController
 @script RequireComponent( CharacterController )
 
 var moveJoystick : Joystick;
 
 var cameraTransform : Transform; // The actual transform of the camera
 var speed : float = 5; // Ground speed     
 
 private var thisTransform : Transform;
 private var character : CharacterController;
 private var velocity : Vector3; // Used for continuing momentum while in air
 
 function Start()
 {
     // Cache component lookup at startup instead of doing this every frame    
     thisTransform = GetComponent( Transform );
     character = GetComponent( CharacterController );
 }
 
 function OnEndGame()
 {
     // Disable joystick when the game ends    
     moveJoystick.Disable();
     
     // Don't allow any more control changes when the game ends
     this.enabled = false;
 }
 
 function Update()
 {
     var movement = cameraTransform.TransformDirection( Vector3( moveJoystick.position.x, moveJoystick.position.y, 0 ) );
     movement.Normalize(); // Adjust magnitude after ignoring vertical movement
     
     // position of joystick
     var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
     
     ///
     /// The code I think isn't working
     /// initial code : ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
     ///
     movement *= speed;
     movement.x *= absJoyPos.x;
     movement.y *= absJoyPos.y;
     
     // Apply gravity to our velocity to diminish it over time
     velocity.y += Physics.gravity.y * Time.deltaTime;
     
     
     movement += velocity;
     movement += Physics.gravity;
     movement *= Time.deltaTime;
     
     // Actually move the character
     character.Move( movement );
 }


thanks !!

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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

How to make Dual Joysticks for Android and IOS? 1 Answer

Hello Im making a Game for mobile and want two floating joysticks one on either side of the screen. 0 Answers

Xbox 360 Controller on macbook pro problem 0 Answers

Map mobile joystick to Input.GetAxis("Horizontal") and "Vertical" 1 Answer

help with First Person mobile game 1 Answer


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