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 mnm2317 · Apr 04, 2012 at 10:39 PM · pivottiltpivot-point

Tilt gameboard

I need some 'controls' help for a game that has the arrow keys title a 5x5 board. The pivot point needs to be the dead center of the board and every arrow should move a side (IE: press RIGHT, and the right edge of the board moves down). I mainly need help with the pivot action as I have done some movement with the arrow keys before, but this will be different and I need to be able to make it multi-touch (IE: pressing RIGHT & UP will tilt the board towards the right and top edge (from your perspective)).

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

1 Reply

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

Answer by Kleptomaniac · Apr 05, 2012 at 01:17 PM

Hey there,

Just use Input.GetAxis (Horizontal and Vertical). GetAxis returns float values in the range of -1 to 1, so we can use that horizontal and vertical rotation variables to tilt the game board.

Try something like this:

 var minRotation : float; //Needs to be within a range of 0 and 85
 var maxRotation : float; //Needs to be within a range of 0 and -85
 
     function Update () {
     
            var horRot : float = (Input.GetAxis("Horizontal")) * Time.deltaTime * -20;
            var vertRot : float = (Input.GetAxis("Vertical")) * Time.deltaTime * 20;
     
            var totalHorRot : float;
            var totalVertRot : float;
     
            if (transform.eulerAngles.x + horRot < 180) {
              totalHorRot = transform.eulerAngles.x + horRot;
            } else {
              totalHorRot = transform.eulerAngles.x + horRot - 360;
            }
     
            if (transform.eulerAngles.z + vertRot < 180) {
              totalVertRot = transform.eulerAngles.z + vertRot;
            } else {
              totalVertRot = transform.eulerAngles.z + vertRot - 360;
            }
     
             transform.rotation = Quaternion.Euler(Mathf.Clamp(totalHorRot, minRotation, maxRotation), 0, Mathf.Clamp(totalVertRot, minRotation, maxRotation));
         }

Attach this script to your game board and you're good to go. :)

Hope that helps! Klep

EDIT

Here is the C# version without resetting to 0. I'll update the Javascript version as well:

 public float minRotation; //Needs to be within a range of 0 and 85
 public float maxRotation; //Needs to be within a range of 0 and -85
 
     void Update () {
     
             float horRot = (Input.GetAxis("Horizontal")) * Time.deltaTime * -20;
             float vertRot = (Input.GetAxis("Vertical")) * Time.deltaTime * 20;
     
            float totalHorRot;
            float totalVertRot;
     
            if (transform.eulerAngles.x + horRot < 180) {
              totalHorRot = transform.eulerAngles.x + horRot;
            } else {
              totalHorRot = transform.eulerAngles.x + horRot - 360;
            }
     
            if (transform.eulerAngles.z + vertRot < 180) {
              totalVertRot = transform.eulerAngles.z + vertRot;
            } else {
              totalVertRot = transform.eulerAngles.z + vertRot - 360;
            }
     
             transform.rotation = Quaternion.Euler(Mathf.Clamp(totalHorRot, minRotation, maxRotation), 0, Mathf.Clamp(totalVertRot, minRotation, maxRotation));
         }

Comment
Add comment · Show 19 · 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 Kleptomaniac · Apr 05, 2012 at 01:51 PM 1
Share

Sorry, completely forgot that the pivot point of a mesh is default in the dead centre, so no need for an empty gameObject. Edited answer :)

avatar image mnm2317 · Apr 07, 2012 at 06:06 PM 0
Share

Wow, thanks, I'm glad I wasn't forgotten about :D. However, I'm getting a fist full of errors now. O.o The ":" and "float" is 'unexpected'. And I can't get past those to see if there's anything else :(.

Also, in order to control the tilt (like pressing an arrow key) do I just do something like this?

if (Input.Get$$anonymous$$eyDown("right")) { horRot + 1; //or horRot++ }

avatar image Kleptomaniac · Apr 08, 2012 at 08:01 AM 1
Share

Actually I could probably try converting to C# if you wanted to ... not my native language so it'll be touch and go ... I'll give it a go ...

avatar image Kleptomaniac · Apr 08, 2012 at 09:51 AM 1
Share

Done. This works in C# and doesn't reset to 0. Took me a while to get past heaps of annoying $$anonymous$$athf.Clamp issues. :)

 void Update () {
 
         float horRot = (Input.GetAxis("Horizontal")) * Time.deltaTime * -20;
         float vertRot = (Input.GetAxis("Vertical")) * Time.deltaTime * 20;
         
         float totalHorRot;
         float totalVertRot;
         
         if (transform.eulerAngles.x + horRot < 350) {
             totalHorRot = transform.eulerAngles.x + horRot;
         } else {
             totalHorRot = transform.eulerAngles.x + horRot - 360;
         }
         
         if (transform.eulerAngles.z + vertRot < 350) {
             totalVertRot = transform.eulerAngles.z + vertRot;
         } else {
             totalVertRot = transform.eulerAngles.z + vertRot - 360;
         }
         
         transform.rotation = Quaternion.Euler($$anonymous$$athf.Clamp(totalHorRot, -5.5f, 5.5f), 0, $$anonymous$$athf.Clamp(totalVertRot, -5.5f, 5.5f));
     }
avatar image Kleptomaniac · Apr 09, 2012 at 09:27 AM 1
Share

Yeah, that's something to do with my conversion of the rotation into negative numbers. I can't for the life of me figure out how to fix it, so what I would suggest is ins$$anonymous$$d of using -90 and 90, use -85 and 85. :) Then it works properly.

Just hinting, but I'd love an upvote for this one ... it's taken me a lot, including learning C# syntax ... :D

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Limited tilt ojbect on pivotpoint 1 Answer

Devil May Cry style Lock-on Movement? 2 Answers

Freeze object rotation on Polybrush 1 Answer

How do I rotate in the center after changing the position of the pivot? 1 Answer

temporarily change the pivot, scale, pivot back 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