Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Raistlin2015 · Feb 20, 2018 at 01:29 AM · vector3eulerangles

How do I return a float value between 1 and -1 based on a camera's rotation?

I have a have a ball with a rigidbody attached and I am adding torque to the ball based on the angle of a third person camera witch rotates around the ball. I am trying to change the direction of the ball based on the position of the camera in its rotation around the ball. I am using Vector3.right for forward and reverse torque and Vector3.back for right and left. I have the forward/reverse value, rightVal working but I have been working on the left/right value, backVal for three days and am at a loss. Here is how I am trying to accomplish this.

  void CalcRightVal()
         {
              float degrees = Camera.main.transform.localEulerAngles.y;
             if (degrees > 0 && degrees < 180.0f)
             {
                 rightVal = (-degrees / 360) * 4 + 1;
             }
     
     
             if (degrees > 180.0f && degrees < 360.0f) //
             {
                 rightVal = (-degrees / 360) * 4 + 1;
     
                 if (rightVal < -1.0f)
                 {
                     rightVal = (degrees / 360) * 4 - 3;
                 }
             }
     
         }
     
         void ApplyTorque()
         {
             Vector3 dir = new Vector3(rightVal, 0, backVal);
             ball.AddTorque(dir * ballAcceleration);
         }
 

In ApplyTorque() I need the backVal to go from 0 to -1 i.e. Vector3.back when degrees == 90. Then backVal should increase in value to 0 when degrees == 180 then up to 1 when degrees == 270 then back to 0 when degrees == 360. This will apply torque to the right when the camera is at 90 degrees and left when the camera is at 270 degrees. The Idea is that the user will add input to forward i.e. KeyCode.W to add acceleration and reverse or KeyCode.S for breaks. The camera will be responsible for changing the direction of the force. I am not terrible at math but this one has me beat.

Here is a graphic to help out. alt text Thank You.

quadrant.png (13.3 kB)
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
0

Answer by Alanisaac · Feb 20, 2018 at 01:47 AM

I could be misinterpreting, but the way you describe it, it sounds like the end goal is to get a 2D normalized vector in the direction from the camera to the ball, and then apply ballAcceleration amount of torque in that direction. This doesn't necessarily answer your original question, but I'm guessing there's a better way to avoid doing all that math yourself. Would the following work?

 void ApplyTorque()
 {
     var cameraPosition = Camera.main.gameObject.transform.position;
     var direction = ball.position - cameraPosition;
     var planarDirection = new Vector3(direction.x, 0, direction.z).normalized;
     ball.AddTorque(planarDirection * ballAcceleration);
 }

EDIT: based on the comment below, it sounds like you want a vector 90 degrees off from the direction the camera is facing. You can do that by getting a normal/perpendicular vector to the camera's direction. In that link, you'll notice you need two vectors representing your plane to get a normal. In your case we can use the vector from the camera to the ball as the first vector, and "up" as the second:

 void ApplyTorque()
 {
     var cameraPosition = Camera.main.gameObject.transform.position;
     var direction = ball.position - cameraPosition;
     var planarDirection = new Vector3(direction.x, 0, direction.z);
     var perpendicularDirection = Vector3.Cross(planarDirection, Vector3.up).normalized;
     ball.AddTorque(planarDirection * ballAcceleration);
 }

I'm still not clear on which direction the torque vector is rotated from the camera (clockwise or counterclockwise) -- BUT if you find this script works almost perfectly except backwards, just reverse the perpendicularDirection by making it negative.

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 Raistlin2015 · Feb 20, 2018 at 03:29 PM 0
Share

As I look over my question I notice that I failed to lay out a key parameter. While rightVal is at 1 or -1, backVal needs to be at 0 and vice versa. That way when the camera is directly behind the ball or 0 degrees the torque applied will be Vector3.right or (1, 0, 0) and when the camera is at 90 degrees or on the left of the ball, torque applied will be Vector3.back or (0, 0, -1). This will apply torque to make the ball turn right. Basically wherever the camera is the ball rolls forward from the point of view of the user. Thank you for the help, and all help is always appreciated.

alt text

cameravalues.png (18.9 kB)

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

146 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 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

Avoiding Gimbal Lock with Vector3 EulerAngles and Applying Torque 0 Answers

Inspactor Roation to Vector3 0 Answers

Why isn't my steering wheel working? 0 Answers

How can I deal with eulers 1 Answer

Rotate object towards Vector 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