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 Freaking-Pingo · Jan 22, 2013 at 09:56 AM · rotationplayermathcontrolsgetaxis

Using GetAxis("...") for making 180 degree controls

Hi there Uniteers, I am in need of your guidance with some player controls for a third person game.

I am in the process of creating a character control script, which can rotate a character around himself 180 degrees depending on the values from

GetAxis("Horizontal")["W" and "S"] and GetAxis("Vertical")["A" and "D"].

The idea with this, is when the user right click his mouse, the camera will be "locked" on the character, and the character will look straight forward (0 rotation) upon right clicking. The player is then capable of rotating his character with the WASD keys. Depending on what key(s) are pushed he should rotate towards that corresponding direction.

                                   W: 0

                 W+A: -45               W+D: 45

            A:-90                                      D: 90

                S+A: -135               S+D: 135

                              S:180/-180

The "Compass" above is how I would like the system to look like. When the "amount of degree" have been found, it will be fed to transform.RotateAround(transform.position, transform.up, rotateFloat), and the character should look in that direction. I have made sure even after the rotation, the compass is still the same.

The main issue with this problem is that I can't figure out the mathmathical design for this approach using the GetAxis components. Somehow the ranging values (0...1) from both Axis should be able to give me the rotating degree. Some pseudo code:

 rotateFloat = Input.GetAxis("Horizontal")*someMath + Input.GetAxis("Vertical")*someMath

There is the option of simply hard coding every state, but I am not very fond of hard coding, especially not controls.

I hope some of you are capable of helping me. Feel free to ask more questions, or if you wish me to make myself more clear.

Comment
Add comment · Show 3
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 Freaking-Pingo · Jan 22, 2013 at 10:23 AM 0
Share

Yeah, I might be able to use some of that code to make the rotation look prettier, but that really doesn't resolve my issue. I was about to tell you that I can't use that piece of code, but taking a second look at it, it might actually be what I am looking for. I'll play around with it :)

avatar image Freaking-Pingo · Jan 22, 2013 at 10:43 AM 0
Share

$$anonymous$$any, many thanks - Pleas turn your comment into an answer, so I can make it solved, and post my solution :)

avatar image cdrandin · Jan 22, 2013 at 10:28 PM 0
Share

alrighty, glad I can help.

2 Replies

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

Answer by cdrandin · Jan 22, 2013 at 10:03 AM

What you want is something similar to this, which I took from one of Unity's tutorials. transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.direction), Time.deltaTime * movement.rotationSmoothing);

This moves the character over to some direction, over the rotationSmoothing time. You won't need Quaternion.LookRotation, but you will need the Slerp.

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
1

Answer by Berenger · Jan 22, 2013 at 10:46 AM

That's when you realize, trigonometry is a beautiful thing. You have two inputs between -1 and 1, and you want an angle. It's like trigo was made for that question. Here is a way I can think of :

First, make sure to round the inputs so you have only three values possible, -1 0 and 1.

Then, calculate the angle from the x axis with Acos, you'll get PI, PI / 2 or 0. for y, use Asin and get 3PI / 2, 0 or PI / 2.

Finally, calculate the average, convert to degree, and there you go !

Comment
Add comment · Show 1 · 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 Freaking-Pingo · Jan 22, 2013 at 10:53 AM 0
Share

I must say, that could possibly have been a solution. Though cdrandin's solution only involves program$$anonymous$$g rather than math, and my program$$anonymous$$g skills are greater than my math skills :). I am sure if someone else come across this question in need of help, they should look towards this solution as a possible solution.

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

11 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

Related Questions

Mouse Orbit Script with mecanim & turning character 0 Answers

Camera rotates around player but player movement is incorrect 1 Answer

Rotation problem: Check is failing/ endlessly spins 1 Answer

Make 3D Player look exactly at mouse with ScreenToWorldPoint? (maths Question) 2 Answers

Rotate an Object by a spezific value 2 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