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 mkjrfan · Dec 04, 2013 at 10:41 PM · raycastraytrigonometryline drawing

Having trouble finding angle of the object.

I've been trying to get this code to work for an object to draw a line to the player based on the players current location. This script is going to be used for the enemyAi to follow the player so I'm going to use dir (which is the angle that the object is looking at) and then use the translate the object by using the Sin(dir) spd; and the Cos(dir) spd;

My Code:

 dir =Mathf.Deg2Rad * Vector3.Angle(player.transform.position - transform.position,Vector3.forward);
 Debug.DrawRay(transform.position,new Vector3(Mathf.Sin(dir),0,Mathf.Cos(dir))*sightRange);
 
 //To move the enemy object
 amountToMove.x = Mathf.Sin(dir)*spd*Time.deltaTime;
 amountToMove.z = Mathf.Cos(dir)*spd*Time.deltaTime;


 

But for some reason it either doesn't work and points in a different direction or only works at certain spots.

Any reasons why this is happening?

Comment
Add comment · Show 11
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 mkjrfan · Dec 05, 2013 at 01:22 AM 0
Share

Any thoughts or any extra info that I might need to add?

avatar image JChilton · Dec 05, 2013 at 01:25 AM 0
Share

What are you trying to find the angle of in comparison to what position or direction?

avatar image mkjrfan · Dec 05, 2013 at 01:27 AM 0
Share

I have one object and the player. What I need to do is find the angle between the object and the player

avatar image JChilton · Dec 05, 2013 at 01:29 AM 0
Share

To get the angle you need two vectors, the distance between the two items is one vector. What do you want the other to be? the direction the player is facing?

avatar image JChilton · Dec 05, 2013 at 01:30 AM 0
Share

Also, do you actually want an angle, or do you want the vector/distance between the two?

Show more comments

1 Reply

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

Answer by robertbu · Dec 05, 2013 at 01:39 AM

Angles in 3D space take more thought than angles in 2D space. You have to ask, "angle relative to what?" In addition angles you get back are unsigned. You have to provide a reference and some additional code to get a signed angle.

Typically the kind of problem you are outlining (drawing a line between points), is handled by vectors. If you outline how you are drawing lines, then it is likely we can give you a simple solution that avoid complicated angle calculations.

Given what I can see of your code, if you need to plow ahead with angles, take a look at Mathf.Atan2().

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

17 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

Related Questions

Changing position of a RayCast 1 Answer

Detect if raycast hit the specific object. 1 Answer

Physics.Raycast intermittently stops working 1 Answer

Raycast on touch 3 Answers

Problem with raycasting when checking neighbouring objects? 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