• 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 Coverttrickster · Apr 25, 2016 at 06:22 AM · rotation detection

How would i track rotational direction from my rotation cordinates?

So I am trying to figure out the best way to track whether my camera is moving left, right, left diagonal down, left diagonal up, right diagonal down, right diagonal up, up, and down so I can animate my sword to the correct attack the direction asks for. Currently my code looks like this -

 if(checkXPos > currentPos.x + moveBoundry && checkYPos > currentPos.y + moveBoundry)
 {
         //Left Diagonal Downwards
 }
 else if(checkXPos > currentPos.x + moveBoundry && checkYPos < currentPos.y - moveBoundry)
 {
             //Left Diagonal Upwards
 }
 else if(checkXPos < currentPos.x - moveBoundry && checkYPos > currentPos.y + moveBoundry)
 {
         //Right Diagonal Downwards
 }
 else if(checkXPos < currentPos.x - moveBoundry && checkYPos < currentPos.y - moveBoundry)
 {
             //Right Diagonal Upwards
 }
 else if(checkXPos > currentPos.x && (checkYPos > currentPos.y + moveBoundry || checkYPos < currentPos.y - moveBoundry))
 {
         slashLeft = true;
 }
 else if(checkXPos < currentPos.x && (checkYPos > currentPos.y + moveBoundry || checkYPos < currentPos.y - moveBoundry))
 {
         slashRight = true;
 }
 else if(checkYPos > currentPos.y && (checkXPos > currentPos.x + moveBoundry || checkXPos < currentPos.x - moveBoundry))
 {
         //Down Slash
 }
 else if(checkYPos < currentPos.y && (checkXPos > currentPos.x + moveBoundry || checkXPos < currentPos.x - moveBoundry))
 {
         //Up Slash
 }
 else
 {
         CancelAnim();
 }

So it already tracks my movement distance and my coordinate point that I'm looking at from one point to the another to make a line and I have check points to tell whether I am moving left or right, up or down from the difference of the two updating coordinates. How should I go at this and if there is a better method please by all means let me know thank you.

Comment
Add comment · Show 2
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 Scribe · Apr 25, 2016 at 10:50 AM 0
Share

by 'camera moving' do you mean 'camera rotating'? $$anonymous$$y thinking is, if your camera is rotating (or moving in fact) that is usually controlled by something the users has already done, like moving the mouse up/down/diagonally, or pressing some combination of keys, so it would make more sense to calculate the motion performed from the source input. Rather than getting the input, using it to move something, then measuring how it moved in order to perform some action, you can skip the middle two steps.

avatar image Coverttrickster Scribe · Apr 25, 2016 at 10:53 PM 0
Share

Yeah I'm recording the cameras rotation x and y values with three different variables. One that records from a start position, one that records where my rotation is currently at, and one that updates to my current rotation after checking the difference of current rotation and the check rotation.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ben-rasooli · Apr 25, 2016 at 02:50 PM

No a better method, but you can clean up your code a bit, it's too confusing! You can nest your conditions to remove some duplication. For example:

 bool movingLeft = checkXPos > currentPos.x + moveBoundry;
 bool movingDown = checkYPos > currentPos.y + moveBoundry;
 bool movingUp = checkYPos < currentPos.y - moveBoundry;
 bool movingRight = checkXPos < currentPos.x - moveBoundry;
 
 if(movingLeft){
     if (movingDown) {
         //Left Diagonal Downwards
     } else if (movingUp) {
         //Left Diagonal Upwards
     } else {
         //Left
     }
 } else if (movingRight) {
     if (movingDown) {
         //Right Diagonal Downwards
     } else if (movingUp) {
         //Right Diagonal Upwards
     } else {
         //Right
     }
 } else if (movingDown){
     //Down
 } else if (movingUp){
     //Up
 }

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 Coverttrickster · Apr 25, 2016 at 10:49 PM 0
Share

I like that! Thanks.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Smooth tilt accelerometer 1 Answer

i need to rotate cube in z axis or x axis one direction at time 0 Answers

Switch out of stereo mode in Cardboard app? 0 Answers

Detect when rotation stops 2 Answers

How to stop UI/Canvas from rotating along with the camera? 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