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 Cirrus · Oct 30, 2012 at 02:41 AM · anglevector3.angle

Trouble calculating angle between an object and mouse position

Hey guys,

I am creating a 2.5d game, where a character who's position is fixed on the screen, fires a projectile to where ever the mouse is located. I only want an angle around the Z axis, so that the projectile is fired along the X and Y. I tried using this code initially which is attached to the character:

angle = Vector3.Angle(transform.position, Input.mousePosition);

When I debugged angle, the values were very odd, there weren't values that I could use to angle my projectiles. I understand that I need to convert the radians to degrees, but when I moved the mouse around the character, the value was between 60 and 90, so something can't be right. Here is a picture which probably explains what I'm trying to accomplish more clearly:

alt text

If I can find out that angle, I can instantiate a projectile at a rotation which is equal to the angle, and add relative force to send it toward the mouse location.

If someone could let me know where I'm going wrong, or state the steps I need to take to accomplish this, that would be greatly appreciated.

Thanks.

angle.png (341.6 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
3
Best Answer

Answer by sparkzbarca · Oct 30, 2012 at 05:01 AM

the mouse exists on the x and y plane, the object exists in three dimensions

your trying to get the angle between a (x,y,z) and (x,y,z = 0?)

you should first find the right z, the right z is the z of the object.

Alternatly you could pretend the object is 2d for the purposes of this calculation.

something like

 vector2.angle(new vector2(object.transform.x, object.transform.y), mouse.position)

That was all just for more info though, your actually doing that in a too complicated way, since your trying to fire from the Red object to the mouse.

The red object is the character and the mouse is what you'd like to fire towards and the slope of the line between them know both points is an easy thing to find.

You can find a direction (aka the slope of a line) which goes from one point to another by subtracting where your want to go from where your going from. this basically makes where you going from 0 and the x,y,z == slope then or the direction to go.

 direction = mouse.position - player.transform.position

you now instantiate a project at player position and use that direction.

Lastly you could do it EVEN MORE SIMPLY.

create an object and use the lookat function. That makes the front of the object looks at a given point. It does it how i told you but unity does the math for you.

projectile.lookat(mouse.position)

now the direction to travel is forward with transform or add force going toward projectile.forward with physics.

Since your using a 3d engine but making a 2d game, get the mouse position and set the z depth equal to the objects z depth as stated earlier.

vector3 MousePos = new vector3(mouse.position.x, mouse.position.y, player.transform.position.z)

click the checkmark under the down arrow and mark as answered. good luck!

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

10 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

Related Questions

Vector3.Angle outputs different results for the same values 0 Answers

how detect the angle of collision on disc object ? 1 Answer

Axis are not rotated as expected 0 Answers

Vector3.Angle returning wrong values for vectors with small components 2 Answers

Debug.Log(Vector3.Angle(ray.direction,hit.normal)); HELP! 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