• 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 AlejandroGorgal · Nov 19, 2013 at 07:48 PM · rotationmobilejava

Rotate character towards direction?

Hi, Im trying to get a character to move and rotate towards the input received from a virtual joystick, I've got the movement working but I cant figure out how to make the character rotate in that direction. Here's the code I've got so far:

 // Setting up some basic variables
 var speed : float = 2.0;
 var rotationSpeed : float = 100.0;
 var gravity:float = 500.0;
 
 private var moveDirection:Vector3 = Vector3.zero;
 
 // This var will request for a joystick input on the inspector
 var JoystickScript:Joystick; 
 // Displays the Jostick Pos on the inspector
 var JoystickPos:Vector2;
 
 
 function Update () 
 {
     //Debug.Log(JoystickScript.position);
     // We take the position value from the Joystick script and turn it into a var called "JoystickPos"    
     JoystickPos = JoystickScript.position; 
     
     // Looks for the controller component
     var controller : CharacterController = GetComponent(CharacterController); 
     
     var translationY : float = JoystickPos.y;
     var translationX : float = JoystickPos.x;
     var rotationY : float = JoystickPos.y * rotationSpeed * Time.deltaTime;
     var rotationX : float = JoystickPos.x * rotationSpeed * Time.deltaTime;
     var rotation : float = JoystickPos.x * JoystickPos.y;
         
     // If the Joystick is moved then it plays the run animation
     if (controller.isGrounded && JoystickPos.y > 0.5 || JoystickPos.y < -0.5 || JoystickPos.x > 0.5 || JoystickPos.x < -0.5)
     {
     animation.CrossFade("RunF"); // Plays running animation
     moveDirection = Vector3(translationX, 0, translationY); // Allows player input
     moveDirection = transform.TransformDirection(moveDirection); // How to move
     moveDirection *= speed; // How fast to move
     //transform.Rotate (0, rotation, 0); // Rotates the character
     }    
     else
     {
     animation.CrossFade("Idle");
     moveDirection = Vector3(0, 0, 0); // Stops the character from moving when input is released
     moveDirection = transform.TransformDirection(moveDirection); // How to move
     moveDirection *= speed; // How fast to move
     }
     
     // Applies gravity
     moveDirection.y -= gravity*Time.deltaTime;
     
     // Moves the controller
     controller.Move(moveDirection * Time.deltaTime);
 }
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 AlejandroGorgal · Nov 20, 2013 at 03:09 AM 0
Share

I've started experimenting with the "Transform.LookAt" function which seems like it may work but I cant figure out which value to add as the target for it.

Any ideas?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by supernat · Nov 20, 2013 at 05:04 AM

I think you could use the LookAt function by passing the player's position plus the move direction.

transform.LookAt(transform.position + moveDirection);

Or, if that doesn't work in your setup, you can calculate the angle using trig.

float rotation = Mathf.Atan2(moveDirection.z, moveDirection.x);

If the camera rotates with the player, this won't work without additional consideration, specifically you would need to add the current rotation in. I mean if the camera is always following the player.

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 AlejandroGorgal · Nov 20, 2013 at 05:26 PM 0
Share

Firs of all, thanks a lot for the reply, I've tried the first bit of code you suggested but when I test it the character starts spinning around incredibly fast when I press any direction.

As for the second bit, Im not sure where to add it, Im sorry, this is my first time trying to code (Im a character artist trying to learn coding), if I add it to the end of the function it gives me an error saying it's expecting a semicolon at the end even though it has one already.

Now, I did some experimenting and I think I've found something that could work, which is this code:

 transform.LookAt(JoystickPos);

Only problem with this is that this code gives me coordinates for the X and Y axis and I somehow need to convert those Y coordinates into Z so that I can rotate the character correctly.

avatar image supernat · Nov 20, 2013 at 06:32 PM 0
Share

That's interesting that the character spins. I wasnt really sure if that first bit would work, sorry. See below for another change that might fix that.

For the second code, are you setting the charcter's rotation angle or adding to it? I think if you just set te y value of the eulerAngles for the transform, it should point the character in the direction of the two axis joystick input, but maybe there is something I'm missing. I'm trying to write this on my phone, but I'll try to test this out tonight.

Using lookAt with the joystick inputs directly will/should work if the player position stays at 0,0,0 although as you pointed out, you would need to swap the y and z values. But once the player moves, I would expect the code would cause the player to always look back near the origin. That's why I added the player's position in that first snippet you tried that didn't work.

Swapping is easy, just create a temp vector and swap the inputs from the joystick, like you do for moveDirection.

$$anonymous$$aybe the joystick values you get back are too small. Try multiplying the moveDirection by 100 in the first snippet I sent. $$anonymous$$aybe the player won't spin.

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

18 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

Related Questions

2.5D Mobile joystick rotation problem 0 Answers

Turning touch to rotation 1 Answer

Make gameObject face position of Joystick [Mobile] 4 Answers

Mobile rotation on pivot 2 Answers

[JS] Custom camera rotation resets on Var change? 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