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
1
Question by MateusSarmento · May 11, 2015 at 11:50 AM · mathf.sinsincoscosinemathf.cos

Using sin and cos to decompose a vector

I want to decompose a vector, being the rotation.y and a float force, and assign to x and z floats. So I made this code:

 transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
     
 float rot = transform.rotation.y;
             
 float x = force * Mathf.Cos(rot)* Input.GetAxis("Vertical") * Time.deltaTime;
 float z = force * Mathf.Sin(rot)* Input.GetAxis("Vertical") * Time.deltaTime;
            
 rigidbody.AddForce(new Vector3(x,0,z));

I see some problems that are: rotation varies between 0 and 1 instead of 0 and 360 and Cos and Sin methods return in radian. I can not solve that problem. Can someone help?

The result that I want to reach is make a object go to where is facing. Decomposing the force will move the object in x axis and z axis.

Comment
Add comment · Show 1
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 Bunny83 · May 11, 2015 at 12:13 PM 0
Share

Uhm, if you want the forward direction (z direction) you have your sin / cos reversed ^^ You calculate the "right" direction (x direction). Also your rotation will rotate in the wrong direction.

To get the forward direction use:

 x = $$anonymous$$athf.Sin(y_Radians)
 z = $$anonymous$$athf.Cos(y_Radians)

To get the right direction you have to use

 x = $$anonymous$$athf.Cos(y_Radians)
 z = -$$anonymous$$athf.Sin(y_Radians)

Note the "-".

But as already mentioned using transform.forward or transform.right is usually way simpler ^^

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by tanoshimi · May 11, 2015 at 11:51 AM

transform.rotation.y gives you the y component of a four-dimensional quaternion rotation. I suspect you wanted transform.rotation.eulerAngles.y.

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 Bunny83 · May 11, 2015 at 12:02 PM 0
Share

But don't forget $$anonymous$$athf.Deg2Rad since $$anonymous$$athf.Sin takes an angle in radians while eulerAngles are in degree

$$anonymous$$ateusSarmento:
ps: transform.right actually gives you that vector that faces to the right of the object just like the one you calculated. $$anonymous$$eep in $$anonymous$$d that transform.right is affected by all rotations of this transform. You're method only rotates around the world y axis.

avatar image
0

Answer by MateusSarmento · May 11, 2015 at 09:04 PM

Yeah, thanks man! I got it. The right code is:

  transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
      
  float rot = transform.eulerAngles.y * Mathf.Deg2Rad;
              
  float x = force * Mathf.Cos(rot)* Input.GetAxis("Vertical") * Time.deltaTime;
  float z = force * Mathf.Sin(rot)* Input.GetAxis("Vertical") * Time.deltaTime;
             
  rigidbody.AddForce(new Vector3(x,0,z));

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Cos creates problems with addforce 0 Answers

Camera Rotation 1 Answer

What's wrong with this algorithm? 2 Answers

Move and Rotate player in circular motion? 2 Answers

How can I get a point position in circle line 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