• 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 MerlinsMaster · Mar 04, 2015 at 05:30 AM · trajectory

How to determine a game object's trajectory?

Hey Guys,

I have a spaceship in my game which uses Newtonian physics (addForce to move forward and AddTorque to turn), and I'm trying to figure out how to determine what direction it is actually moving in. What's the easiest way to determine this in degrees?

I know it's a vector3, but a Vector3 produces three values. I'm mathematically challenged, so what is a simple way in C# to convert it to a single value in degrees (all motion is occurring on the X/Z plane, so the Y value would be left out).

Comment
Add comment · Show 3
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 AlwaysSunny · Mar 04, 2015 at 04:42 AM 0
Share

"Direction in degrees" - I assume you mean, "what angle is swept by my object's forward direction versus the world coordinate system".

If so, the context tells me this isn't a strong basis for your core game mechanics. $$anonymous$$ost of your logic can and should be based around vector arithmetic. Further, you rarely need to work with "degrees" in most cases. $$anonymous$$ost rotation / orientation logic can and should be driven by vectors and quaternions and how they relate to each other. You'll pick this stuff up as you go. In the mean time:

To get the actual direction of motion from a rigidbody, you can normalize its velocity

 Vector3 actualDirectionOf$$anonymous$$otion = rigidbody.velocity.normalized;

To get a meaningful "angle" from this value, compare it to your preferred axis:

 float angle = Vector3.Angle(actualDirectionOf$$anonymous$$otion, Vector3.forward);

avatar image MerlinsMaster · Mar 05, 2015 at 06:34 AM 0
Share

Thanks, that's very helpful. I hate to be that guy who's just asking for the answer, but there's one thing I'm having trouble with.

In my EnemyAI script, I need the enemy ship to face the same direction the player ship is traveling in. So, my question is, if this is the line of code that causes the enemy ship to turn directly toward the player ship...

  myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotateSpeed * Time.deltaTime);

...then how could I modify this code to tell the enemy ship to turn to the actualDirectionOf$$anonymous$$otion?

I understand it has to do with vectors and quaternions and how they relate to each other, like you were saying, but I'm having trouble figuring out how that would work in this context.

The first thing I tried was to take my newly defined actualDirectionOf$$anonymous$$otion, and tried to replace the second value in the line with it. But that clearly did not work, because Unity needs two Quaternions and a float for that operation.

avatar image AlwaysSunny · Mar 08, 2015 at 06:50 AM 0
Share

Sorry for the late reply. UnityAnswers' notification system is completely hit-or-miss for me.

I can't quite tell, but it sounds like you're trying to create an "intercept" behavior. The simplest form of this involves adding some factor of the target's velocity to a "seek" behavior. There's a great deal of "beginner's steering" info here; I'm confident it will be helpful: http://www.red3d.com/cwr/steer/

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Setting limits to my trajectory 2 Answers

trajectory of ball 0 Answers

Bezier curve that update in real time. 1 Answer

Trajectory Prediction with Particle Systems 1 Answer

Calculate a trajectory in all possible directions (2D). 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