Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 FakeBocha · Jan 04, 2018 at 07:12 PM · scripting problemmath

Help with some math!

Hi guys!

Umm i have this Blend tree: ![alt text][1]

I want to make the red dot to be my enemy! The player is always facing towards the enemy, I used this code to get the angle between where the player is facing and the key press.

 Vector3 direction = new Vector3 (iH.horizontal, 0, iH.vertical);
         direction = Camera.main.transform.TransformDirection (direction);
         direction.y = 0f;
 
         float angle = Vector3.SignedAngle (transform.forward, direction, Vector3.up);

but i cant find the solution for making the red dot be where the enemy location is... I tried this:

         Vector2 relativeToEnemy = new Vector2 (Mathf.Cos (temp), Mathf.Sin (temp));
 

but for some reason it has a weird behavior to it...

Please help me! I'm on this problem for 4 days!! [1]: /storage/temp/108569-צילום-מסך-20180104-ב210541.png

צילום-מסך-20180104-ב210541.png (47.7 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
1
Best Answer

Answer by $$anonymous$$ · Jan 04, 2018 at 07:53 PM

angle between where the player is facing and the key press

Sorry i don't understand which angle you want to measure exactly or at least that last part. However, if you have made sure the "angle" variable is calculated correctly, your "relativeToEnemy" vector shouldn't be far off. It's a pity your code doesn't show how your "angle" variable translates to your "temp" variable (and the naming isn't helping either). I guess you have already multiplied it with Mathf.Deg2Rad?
Comment
Add comment · Show 3 · 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 FakeBocha · Jan 04, 2018 at 08:57 PM 1
Share

Thanks for the reply! Oh that temp is a mistake - it waas only for testing!

This is how it supposed to look like: Vector3 direction = new Vector3 (iH.horizontal, 0, iH.vertical); direction = Camera.main.transform.TransformDirection (direction); direction.y = 0f;

         float angle = Vector3.SignedAngle (transform.forward, direction, Vector3.up);
 
         Vector2 relativeToEnemy = new Vector2 ($$anonymous$$athf.Cos (angle), $$anonymous$$athf.Sin (angle));

I am measuring the angle of: 'player.forward' (my forward is always facing the enemy) TO 'my input relative to world space'. But my angle is not the issue - it works great!

I cant find a way to make the angle translate to the blend tree.

Whats $$anonymous$$athf.Deg2Rad? and how should I use it?

avatar image FakeBocha · Jan 04, 2018 at 09:02 PM 1
Share

!!!!

This worked! I dont realy understand what it did - if you can help me with that I would be grateful!

But neither way - Thank you!!

Final Code!

 Vector3 direction = new Vector3 (iH.horizontal, 0, iH.vertical);
         direction = Camera.main.transform.TransformDirection (direction);
         direction.y = 0f;
 
         float angle = Vector3.SignedAngle (transform.forward, direction, Vector3.up);
         angle = angle * $$anonymous$$athf.Deg2Rad;
 
         Vector2 relativeToEnemy = new Vector2 ($$anonymous$$athf.Cos (angle), $$anonymous$$athf.Sin (angle));
avatar image Bunny83 FakeBocha · Jan 04, 2018 at 09:40 PM 1
Share

In mathematics we measure angles in radians. While most end user calculators which have trigonometric functions work with angles in degrees, almost all program$$anonymous$$g languages and in scientific context we always work with radians.


One full circle in Radians is 2*PI. It represents the length of the arc on the unit circle (circle with radius "1").


$$anonymous$$ost of Unity's API methods work with angles in degrees because they are easier to work with in user interfaces. So Quaternion.Euler, Quaternion.AngleAxis, ... all take angles in degree and Vector3.Angle, Vector3.SignedAngle return angles in degree.


However the pure mathematic / trigonometric functions (Sin. Cos, Tan, ...) always work with radians. To convert degrees to radians you just have to divide by 180 and multiply by PI. Therefore $$anonymous$$athf.Deg2Rad is simply the constant $$anonymous$$athf.PI / 180f. Likewise converting radians to degree is just the reverse. So $$anonymous$$athf.Rad2Deg is just the constant 180f / $$anonymous$$athf.PI

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

127 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

Related Questions

How to build a car script that allows for car to turn in full circles 1 Answer

Clamp a Vector above minimum angle from another Vector? 0 Answers

Need help rendering Orbits, with Orbital Elements 1 Answer

Calculating jump while rotating around pivot 1 Answer

How to get the "Perfect" Sine Wave Pattern 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