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 Tuah · Nov 22, 2010 at 09:00 PM · mathvectorsimple

More simple vector math...

I want to determine a particular object's velocity along a given vector. Let's say I have a cube on its side, and the cube's "Up" is pointing to the left.

I want to find how fast an object is traveling along the cube's up, then stop all movement along that cube's "up" axis.

IE: (velocity along cube's up).y = 0;

How might I do this?

EDIT: I'm working only with Vector3. I have Vector3 cubeUp, and Vector3 objectVelocity, trying to figure out objectVelocity relative to cubeUp. Vector3.Dot doesn't seem to be what I need. Maybe Vector3.Slerp?

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

2 Replies

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

Answer by duck · Nov 22, 2010 at 09:41 PM

You need to use the InverseTransformDirection function here. Let's say "Sphere" is the moving object to test, and you want to find out how fast it's moving along the cube's local "up" axis:

var v = sphere.rigidbody.velocity;
var relativeVelocity = cube.transform.InverseTransformDirection( v );

You can then test "relativeVelocity.y" to find out the sphere's speed along the cube's up axis. If you want to set that to zero, and assign it back to the sphere, you can do this:

relativeVelocity.y = 0;
var newVelocity = cube.transform.TransformDirection( relativeVelocity );
sphere.rigidbody.velocity = newVelocity;

It's worth noting however, that it's generally advised against setting object's velocities directly as it can result in unpredictable and erratic results from the physics engines - so use with caution!

In my experience it seems to be okay if the velocity changes don't interfere with other objects (eg, causing an collision that was not expected by the physics engine).

Comment
Add comment · Show 2 · 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 Tuah · Nov 22, 2010 at 09:49 PM 0
Share

Thanks, I'll test it out!

The actual changing of velocity was just a simplified example of what I'm doing. I'm adding a force with VelocityChange.

avatar image Tuah · Nov 22, 2010 at 11:11 PM 0
Share

NEW: Ah, I see how that works. I'm not sure I can figure out how to do that with a vector3 ins$$anonymous$$d of a transform. I only have a Vector3 that points in the direction of the cube's "Up", not a whole Transform.

avatar image
2
Best Answer

Answer by Bampf · Nov 23, 2010 at 02:26 AM

Dot is in fact what you want. You take the dot product of the Up vector and the velocity's direction - this gives you a number which is the amount of the velocity vector that was going Up. (It will be 1 if they are going the same direction, 0 if right angles to each other.)

For Dot to work you need normalized vectors (i.e. of length 1). Up is already normalized, you just need to use velocity.normalized.

Once you have Dot, you will want to multiply it by the magnitude of the original velocity vector. This gives you a vector pointing Up which is the component of the original velocity that was moving Up.

By subtracting that vector from the original velocity vector, you will end up with a new vector with the Up component cancelled out.

Here is another Answer that is using Dot. (It's only checking whether something is moving towards an object or away from it, so it doesn't do the multiple or subtract steps.) http://answers.unity3d.com/questions/12595/help-with-vector3-dot-please

Yours will look something like this (note: not debugged code.)

Vector3 upComponent = velocity.magnitude * Vector3.Dot(upVector, velocity.normalized);
Vector3 newVelocity = velocity - upComponent;
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 Tuah · Nov 23, 2010 at 06:20 AM 0
Share

Ah, this worked quite well, as far as I can tell thusfar. I used Vector3 upComponent = velocity.normalized * Vector3.Dot(velocity, upVector);

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

Unity is a Left-Handed Coordinate System? Why? 5 Answers

Move Vectors as curve towards Target 1 Answer

How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 Answers

Given a point, a "radius" and an arc, how do I find the resulting point? 1 Answer

Rotate an Object by a spezific value 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