• 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 MirrorIrorriM · Jul 22, 2011 at 02:39 AM · rotationrigidbodyvelocityforcerelative

relative velocity of rigidbody according to rotation...

Hello all. I was messing around with programming tank treads. I'm simply using a texture that slides across a model to simulate treads. Thing is in order to move this I need to "fake" the treads moving along the ground. To do this I decided to just check if the treads are on the ground and use "rigidbody.AddRelativeForce" to move it forward when it is. So everything is working fine except one problem.

I don't know how to check the relative velocity to set a speed limit.

 if (Input.GetKey(Forward)){ //Forward is a custom set key
 if (Physics.CheckCapsule(transform.TransformPoint(0,-.6,-.4),transform.TransformPoint(0,-.6,.4),.09)){//This checks if you are on the ground
     if (rigidbody.velocity.magnitude < 5)//THIS is what I want to replace
         rigidbody.AddRelativeForce(Vector3(0,0,1)*Power);

 }}

Right now I am simply using "rigidbody.velocity.magnitude" to check the speed, but that only gives me the total velocity, I want the velocity relative to the direction the treads are pointing. That way if I am moving at the speed of sound sideways, you can still push yourself forward instead of having to wait for the tank to slowdown so you can move where you want to go. Another problem is if you were being pushed backwards you wouldn't be able to push back which would really suck. Anyway to make the velocity magnitude nice and rotated to only look at one axis of movement relative to the object? Specifically the Z axis?

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

Answer by hellcats · Jul 22, 2011 at 02:53 AM

You could just check the z component (rigidbody.velocity.z) instead of the magnitude.

Comment
Add comment · Show 3 · 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 MirrorIrorriM · Jul 22, 2011 at 02:57 AM 0
Share

rigidbody.velocity.z is relative to the world z axis. I need rigidbody.velocity.z but relative to the OBJECT'S z axis, which has been rotated.

avatar image hellcats · Jul 22, 2011 at 04:07 AM 1
Share

You could use transform.InverseTransformDirection(rigidbody.velocity) to transform the velocity into local space and then check the z component, or more efficiently you could compute the dot product of rigidbody.velocity and transform.forward using Vector3.Dot.

avatar image MirrorIrorriM · Jul 22, 2011 at 05:34 AM 0
Share

That's precisely the code I needed. Thanks!

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

relativeVelocity how to? (noob question) 2 Answers

Rigidbody velocity has got different direction than VelocityChange force vector 1 Answer

Rigid Body relative speed 1 Answer

How to make rigidbody rotation more unstoppable 0 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges