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 TakMarche · Aug 24, 2012 at 01:39 PM · rotationrotatejoystick

Rotate An Object Toward The Direction The Joystick Is Pressed

I've been working with the mobile/android Joysticks for awhile now and I've finally got the script to do what I wanted to do thanks to the wonderful people who helped me here in the community. My next task now is to try and make the "Player Object" face the way the joystick is held (view point is 2D Top View Shooter so you're looking down on the player) by degrees. Like if I push the joystick up left the player faces up left and so forth. I don't know exactly where to begin. Can anyone please lend me a hand?

 //////////////////////////////////////////////////////////////
 // SidescrollControl.js
 //
 // SidescrollControl creates a 2D control scheme where the left
 // pad is used to move the character, and the right pad is used
 // to make the character jump.
 //////////////////////////////////////////////////////////////
 
 #pragma strict
 
 @script RequireComponent( CharacterController )
 
 // This script must be attached to a GameObject that has a CharacterController
 var moveTouchPad : Joystick;
 var fireTouchPad : Joystick;
 
 var forwardSpeed : float = 4;
 var backwardSpeed : float = 4;
 var jumpSpeed : float = 16;
 var inAirMultiplier : float = 0.25;                    // Limiter for ground speed while jumping
 
 private var thisTransform : Transform;
 private var character : CharacterController;
 private var velocity : Vector3;                        // Used for continuing momentum while in air
 private var canJump = true;
 var ftp: boolean = false;
 
 function Start()
 {
     // Cache component lookup at startup instead of doing this every frame        
     thisTransform = GetComponent( Transform );
     character = GetComponent( CharacterController );    
 
     // Move the character to the correct start position in the level, if one exists
     var spawn = GameObject.Find( "PlayerSpawn" );
     if ( spawn )
         thisTransform.position = spawn.transform.position;
 }
 
 function OnEndGame()
 {
     // Disable joystick when the game ends    
     moveTouchPad.Disable();    
     fireTouchPad.Disable();    
 
     // Don't allow any more control changes when the game ends
     this.enabled = false;
 }
 
 function Update()
 {
     var movement = Vector3.zero;
 
 // Apply movement from move joystick
     if (moveTouchPad.position.y > 0)
     {
         movement += Vector3.up * forwardSpeed * moveTouchPad.position.y;
     }
     else
     {
         movement += Vector3.up * backwardSpeed * moveTouchPad.position.y;
     }
     if (moveTouchPad.position.x > 0)
     {
         movement += Vector3.right * forwardSpeed * moveTouchPad.position.x;     
     }
     else
     {
         movement += Vector3.right * backwardSpeed * moveTouchPad.position.x;
     }
     
     // Check for jump
     if ( ftp == true)
     {        
         var jump = false;
         
         var touchPad = fireTouchPad;
             
         if ( !touchPad.IsFingerDown() )
             canJump = true;
                 print("FIRE!!!");
          if ( canJump && touchPad.IsFingerDown() )
          {
             jump = true;
             canJump = false;
          }    
         
         if ( jump )
         {
             // Apply the current movement to launch velocity        
             velocity = character.velocity;
             velocity.y = jumpSpeed;    
         } 
         }
         
     movement *= Time.deltaTime;
     
     // Actually move the character    
     character.Move( movement );
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by Semicolon · Aug 24, 2012 at 03:22 PM

I'm note sure how Unity handles joystick input but looking at the documentation it seems like it gives you a value between -1 and 1 for each axis. You could use trigonometry to get the angle.

Look here: http://docs.unity3d.com/Documentation/Manual/Input.html

The function you're looking for is "Mathf.atan2". Simply give it your X and- Y axis values and it will return the angle (in radians I believe). Then what you can do is multiply that value with Mathf.rad2deg and use it for your character's rotation. It could look like this:

 char.transform.eulerAngles = new vector3(char.transform.eulerAngles.x, Mathf.atan2(x, y) * Mathf.rad2deg, char.transform.eulerAngles.z);

Now, that's C# but you can probably figure out how to write it in JS. If you're not sure how to use the input class, search around (google!) and there'll be a ton of pages helping you.

Best of luck

Comment
Add comment · Show 7 · 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 TakMarche · Aug 25, 2012 at 12:09 AM 0
Share

Thank you for pointing me towards a direction I'll check it out.

avatar image rockyourteeth · Mar 13, 2014 at 04:19 AM 0
Share

Ha, get it? "point you towards a direction". Good one.

avatar image pulkit8.mahajan · Mar 19, 2014 at 10:22 PM 0
Share

I am having a similar problem. But for me, the gun objects rotation keeps moving for as long as the joystick is held, ins$$anonymous$$d of moving toward the direction of the the joystick. For example: if i moved my joystick to the right, ins$$anonymous$$d of stopping when it reaches the right, it continues to spin in a circle. Here is the code:

  var rotatePos = Input.GetAxis ("Horizontal") ? 
                            Input.GetAxis ("Horizontal") : joyStickInput(rotateJoystick);
                            this.transform.Rotate(0, rotatePos * rotateSpeed, 0);

please help

avatar image rockyourteeth · Mar 19, 2014 at 10:34 PM 0
Share

pulkit, I recommend starting a new question, since your situation is unique.

avatar image pulkit8.mahajan · Mar 20, 2014 at 12:36 AM 0
Share

I have. You can find the link here

link text

Show more comments
avatar image
0

Answer by nextage575 · Dec 02, 2019 at 04:44 AM

   float xmove = CrossPlatformInputManager.GetAxis("Horizontal") * carSpeed;
         float zmove = CrossPlatformInputManager.GetAxis("Vertical") * carSpeed;
         rb.velocity = new Vector3(xmove, 0, zmove);
         movement = new Vector3(xmove, 0.0f, zmove);
         transform.LookAt(transform.position + movement);
Comment
Add comment · 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 nextage575 · Dec 02, 2019 at 04:44 AM 0
Share
 float moveHorizontal = CrossPlatformInput$$anonymous$$anager.GetAxis("Horizontal");
         float moveVertical = CrossPlatformInput$$anonymous$$anager.GetAxis("Vertical");
         movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
         rb.AddForce(movement * Time.deltaTime * carSpeed * 100f);
         transform.LookAt(transform.position + movement);
avatar image nextage575 · Dec 02, 2019 at 04:45 AM 0
Share

float moveHorizontal = CrossPlatformInput$$anonymous$$anager.GetAxis("Horizontal"); float moveVertical = CrossPlatformInput$$anonymous$$anager.GetAxis("Vertical"); movement = new Vector3(moveHorizontal, 0.0f, moveVertical); transform.Translate(movement Time.deltaTime carSpeed, Space.World); transform.LookAt(transform.position + movement);

avatar image
0

Answer by JuiceTinGames · Jun 25, 2020 at 03:11 AM

I just want to say thank you so much u/Semicolon, I was literally stuck on this forever, your answer came in clutch man, thanks so much.

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

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

After rotate with joystick, rotate resetting,joystick rotate correction 0 Answers

Player rotating in direction of joystick doesn't work, why?,Rotate Player in direction of Joystick has too little impact 0 Answers

Rotation Jumping values (0 to 180) 1 Answer

Rotating sphere 0 Answers

2d game,rotation problem...and need some help to prevent the camera from following my player when it rotates 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