• 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 Redtwoactual · Oct 08, 2013 at 05:35 PM · vector3velocity

Local Vector

I am trying to achieve a negative value for my down vector. This is the code that I have come up with but it is not working. I only want the y values to be negative when going down, all other values should remain positive. I would like to simulate my aircraft gaining speed as it descends, and slowing as it ascends.

 var horizontalVelocity : Vector3 = chopper.velocity;
 var verticalVelocity : Vector3 = chopper.velocity;
 horizontalVelocity = Vector3(chopper.velocity.x, 0, chopper.velocity.z);
 verticalVelocity =   transform.InverseTransformDirection (Vector3(0,chopper.velocity.y,0));
 var totalVelocity = horizontalVelocity - verticalVelocity;
 GUI.Label( Rect(120, 182, 256, 256 ), (totalVelocity.magnitude * 3.6f) + "Km");
Comment
Add comment · Show 4
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 robertbu · Oct 08, 2013 at 06:08 PM 1
Share

Your code and your statements are confusing. To start with, your first two lines are not necessary (beyond declaring the variables) since you overwrite the values in line 3 and 4. Assu$$anonymous$$g chopper.velocity is in local coordinates, then your transform.TransformDirection() will convert the vector to world space and the 'Y' value will be negative if the object is decending. You are using transform.InverseTransformDirection(), which converts from world to local.

Your totalVelocity calculation on line 5 I don't understand at all. And I'm not sure where you expect to have a negative number, but Vector3.magnitude will always be positive.

Please verify that chopper.velocity is a local coordinate or a world coordinate. Then state clearly what you are trying to calculate.

avatar image Redtwoactual · Oct 08, 2013 at 08:47 PM 0
Share

Thank you very much for your help. I had cobbled the code together from reading the scripting resource from the manual. Removing magnitude from the final code gave me close to the desired effect. What I am trying to do is have the x and z co-ordinates both be positive all the time in all directions, and have them so I can add them together to get a value for my horizontal speed. I would then like to be able to subtract my y value from that sum so that as I gain altitude my total speed value decreases, and as I descend in altitude my total speed value increses. I am relatively new to scripting, but have worked my way through a number of tutorials. my new code:

     horizontalVelocity = Vector3(chopper.velocity.x, 0, chopper.velocity.z);
     verticalVelocity =  Vector3(0, chopper.velocity.y,0);
     totalVelocity = horizontalVelocity + verticalVelocity;
     GUI.Label( Rect(120, 182, 256, 256 ), (totalVelocity * 3.6f) + "$$anonymous$$m");
avatar image Jamora · Oct 08, 2013 at 08:54 PM 0
Share

Are you using the built-in physics of Unity? You could then have e.g. gravity and access the velocity of any rigidbody with rigidbody.velocity. Is chopper the rigidbody?

avatar image Redtwoactual · Oct 08, 2013 at 09:29 PM 0
Share

chopper is the rigidbody, and yes I am using the unity physics.

1 Reply

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

Answer by robertbu · Oct 08, 2013 at 09:04 PM

Given your follow-up comment, I'm guessing that chopper.velocity is in world coordinates. In your code, totalVelocity is the same as chopper.velocity. That is, when you separate out the components as you've done in horizontalVelocity and verticalVelocity, then add them back together, you just reconstruct chopper.velocity. I'm not sure what you want. Velocity is a vector. Even only horizontally it will not always be positive if it is in world coordinates. If your plane is largely flying forward with maybe a bit of left and right velocity in the turns, then may the local velocity might always be positive. But this is a lot of ifs and assumes some unrealistic physics. What you may want is the speed of the plane and then the rate of asset or decent. Assuming chopper.velocity is in world coordinates, you can calculate it this way:

 var groundSpeed = Vector3(chopper.velocity.x, 0, chopper.velocity.z).magnitude;
 var climbRate = chopper.velocity.y;

These will both be floats. 'groundSpeed' will always be positive. 'climbRate' will be negative if the plane is descending, and positive if it is ascending. You could combine them into a string like:

 var output = "Speed: " + groundSpeed  + "  Climb: " + climbSpeed;  

If chopper.velocity is in local coordinates, you will need to do a transform.TransformDirection() first, and then calculate groundSpeed and climbRate off the converted vector. You must convert local to world so that the 'y' component will represent up and down in world space, not along the 'y' axis of the object.

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 Redtwoactual · Oct 08, 2013 at 09:22 PM 0
Share

Cool got it thankyou!

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

16 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using Velocity to Switch Animations 1 Answer

Changing 1 parameter of rigidbody.velocity (Vector3) 2 Answers

2D Enemy chasing player not at a constant speed,Enemy following player at constant speed 0 Answers

how to find forwards,right,up component of a velocity 2 Answers

Shoot ball according to spot mouse gets clicked 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