Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by TMK · Jun 15, 2011 at 02:51 AM · rotatedirectionaxisjoystickangle

Rotating character based on joystick angle, at an angle

Hi!

I'm trying to control and rotate a player character based on the joystick input like e.g. Torchlight and Magicka.

If you hold the joystick up-left, I want the player character to immediately rotate and face that direction and walk to that position, while having the character always being angled at e.g. a 30 degree angle.

If you hold the joystick down-right, the player character should rotate and face that direction and walk there.

The problem I'm having is keeping the player character at the 30 degree angle while rotating him towards the joystick angle. Here's an image trying to explain what I currently have (the "Wrong" one), and what I hope to achieve (the "Right" one):

alt text

Here's the code I currently use, which works for everything BUT does not keep the player at the right 30 degree angle:

 public const float WALK_SPEED = 250.0f;
 
 float axisHorizontal = Input.GetAxis("Horizontal (J)");
 float axisVertical = Input.GetAxis("Vertical (J)");
             
 float translateX = (axisHorizontal * WALK_SPEED) * Time.deltaTime;
 float translateY = (axisVertical * WALK_SPEED) * Time.deltaTime;
             
 // move the player towards the joystick direction
 GoPlayer.transform.Translate(translateX, translateY, 0.0f, Space.World);
 
 // rotate the player towards the joystick direction
 GoPlayer.transform.localRotation = Quaternion.Euler(0.0f, (Mathf.Atan2(axisHorizontal, axisVertical) * Mathf.Rad2Deg), 0.0f);
 
 // reset Z value to a constant value, since it's modified by the above code
 GoPlayer.transform.position = new Vector3(GoPlayer.transform.position.x, GoPlayer.transform.position.y, 100.0f);


Basically, I want what the line below does, as it rotates it correctly at the angle it's currently on, but instead rotate it to the joystick's direction:

GoPlayer.transform.Rotate(Vector3.up (Time.deltaTime 100.0f));

Thanks!

Comment
Add comment · Show 4
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 aldonaletto · Jun 15, 2011 at 04:17 AM 1
Share

In $$anonymous$$agicka, the character moves on the horizontal plane (ZX), but in this script the character goes up and down (Y axis) ins$$anonymous$$d of forth and back. Is it the intended behavior?

avatar image TMK · Jun 15, 2011 at 12:30 PM 0
Share

Hi aldonaetto. The way I'm currently doing it having the 3D player object on top of a 2D map (made using S$$anonymous$$2), so in that regard, I need it to move it along the X/Y axis while keeping the Z axis constant, so it doesn't move behind the 2D map, if that makes any sense :) But I can change the axis of it all though, e.g. make the 2D map be horizontal ins$$anonymous$$d of vertical, whatever makes it work :) Thanks!

avatar image aldonaletto · Jun 15, 2011 at 01:18 PM 1
Share

If I had to make a game like that, I would use the map at the ZX plane and the camera in perspective projection, adjusting its position in order to have a somewhat similar view, and never would try to tilt the characters. But if you want to reproduce the $$anonymous$$agicka style more closely, maybe you have to use the camera in ortographic mode (set it in the Projection field). You can place the map at ZX or XY plane, but I think ZX plane is easier. I think you can get good results without tilting the characters away from the camera. Are you sure you'll need to do that?

avatar image TMK · Jun 15, 2011 at 01:26 PM 0
Share

Doh, that is a much easier solution! I was thinking about it the wrong way :) I will try to do this ins$$anonymous$$d as it should work out well. Thanks aldonaletto!

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

2 People are following this question.

avatar image avatar image

Related Questions

Move and Rotate on a fixed axis? 1 Answer

Making player face the direction it moves (using angles). 1 Answer

3rd person unit control 0 Answers

How to make the npc face the player. 2 Answers

Objects rotate but remain on the original axis... 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