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 DanjelRicci · Mar 31, 2011 at 06:25 AM · anglevector2

Using "rigidbody.AddForce" to throw an object towards a target - iOS

On iOS, I have a wrecked car with a Rigidoby that should be pushed towards a specified target in the space (that target is the position where you touch the screen). The force must be applied only on the XZ plane, so no need to calculate Y height. Actually, I can find correctly the touch point. Consider that my car doesn't need to stop moving on the target, but just to slide towards it until it stops (before or after reaching the target, it doesn't matter).

I wanted to use rigidbody.AddForce for this task. Since the direction of must be expressed with a Vector3, i can't specify the precise target position but I have to calculate the angle between the car and the touch point. And here comes the problem: looks like I can't find this angle, no matter how I try.

I tried using this:

var myAngle = (Vector2.Angle(Vector2(carPos.x,0,carPos.z),Vector2(touchPos.x,0,touchPos.z)))*Mathf.Deg2Rad;

rigidbody.AddForce(Mathf.Cos(myAngle),0,Mathf.Sin(myAngle));

This is giving really wrong results. I tried to get myAngle with other three different math functions (wich I found on the web, one was also suggested by my Math teacher), but nothing worked, the angle is always extremely wrong, small, sometimes I get NaN errors.

So, this is the question: how can I calculate the angle between point A and B, relative to the X axis, on an XZ plane? Please consider I can't rotate my car towards that point, it does need to mantain its actual rotation (leaving it to rotate again if it hits something).

Many thanks to everyone who can help. :)

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
2
Best Answer

Answer by Jesse Anders · Mar 31, 2011 at 06:38 AM

Vector3.Angle() returns the angle between two vectors, not the 'directional' angle from one point to another.

The angle you want can be computed using other means, but for this, you don't need to use angles at all. To compute the direction for the force, simply subtract the starting position from the target position and normalize the result, e.g. (untested):

Vector3 direction = targetPosition - startPosition;
direction.Normalize();
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 DanjelRicci · Mar 31, 2011 at 07:43 AM 0
Share

It's working perfectly, thank you so much! :D

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

No one has followed this question yet.

Related Questions

Constructing a Vector in 2-space from an angle for camera panning 1 Answer

Get angle between 2 Vector2's 5 Answers

Weird angle returned 1 Answer

How to get input as a Vector2 from a direction 0 Answers

Adjusting an angle of a collider2D to a linerenderer that is user-created 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