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 $$anonymous$$ · Jun 30, 2013 at 11:20 PM · camerarigidbodyvelocitylocalglobal

Local Velocity for Rigidbody?

I was the guy that asked the Asteroid physics question a few days ago. Basically we solved it, the reason the Asteroids weren't being knocked away by the ship, is because I was translating it, instead of adding velocity and force to the rigidbody.

So now I have an additional problem that has come as a result of solving that initial issue:

This is the result I want = http://youtu.be/SA14X_3ggQ4?t=3m51s I want my camera to be able to pan around the environment on an animated track, and my ship alongside all of the action taking place on the X and Y axis of the camera.

I've decided the easiest way to do this was to take all my assets that were being instantiated, and make them children of the main camera so they would follow it and maintain rotation etc.

That all worked fine when I moved my ship using transform.localtranslate (or the equivalent), and everything worked the way I wanted it to. For extra flair, I wanted to have asteroids in the first level that the player ship could push away with their ship, and later on ships would explode and the debris would bounce off the player ship and collapsing bricks from buildings etc would all interact with the player ship.

So then after solving the rigidbody issue (I had to use velocity instead of just transforming the object), sure we solved the problem of other rigidbodies not behaving with the ship properly, but now the ship won't move along the camera's x and y axis, and just moves to the world's axis again (because I'm now using velocity).

Any way to create local velocity for the ship, and what would be the best way to kill velocity once it has been set. So far I just put in Update() to set the velocity to 0 first before doing key checks, so that if no keys are pressed, the system resets the velocity to 0 (Standing still). Is that the best way to do it? It works so :S

So basically, how to create and maintain local velocity?

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

Answer by DavidDebnar · Jul 01, 2013 at 08:34 AM

Here are the steps:

  1. Take player's input and assign it to a localPos, variable representing ship's position in camera's local space

  2. Calculate camera viewport's bounds in camera's local space. ViewportToWorldPoint(0,0) is bottom-left, ViewportToWorldPoint(1,1) is top-right. These values are in world coordinates, so they have to be converted to local with Cam.InverseTransformPoint().

  3. When the min and max variables are set, clamp the local position.

  4. Lastly, convert the localPos into world coordinates with TransformPoint() and assign it to the rigidbody via rigidbody.MovePosition(). Last thing is to use rigidbody.MoveRotation(Cam.rotation) to make the ship face the right way.

All of this has to happen in FixedUpdate() to make all the MovePosition's collisions/forces behave correctly.

P.S.: The ship is not a child of the camera, because Rigidbodies don't like to be children of moving objects ;).


 private var localPos = Vector3.zero;
 var Speed = 0.2;
 var DistanceFromCamera = 10.0;
 var Cam : Transform;
 
 function FixedUpdate () {
     localPos.x += Input.GetAxis("Horizontal") * Speed;
     localPos.y += Input.GetAxis("Vertical") * Speed;
     localPos.z = DistanceFromCamera;
 
     var min : Vector3 = Cam.InverseTransformPoint(Cam.camera.ViewportToWorldPoint(Vector3(0,0,DistanceFromCamera)));
     var max : Vector3 = Cam.InverseTransformPoint(Cam.camera.ViewportToWorldPoint(Vector3(1,1,DistanceFromCamera)));
 
     if(localPos.x - transform.localScale.x/2 < min.x) localPos.x = min.x + transform.localScale.x/2;
     if(localPos.x + transform.localScale.x/2 > max.x) localPos.x = max.x - transform.localScale.x/2;
     if(localPos.y - transform.localScale.y/2 < min.y) localPos.y = min.y + transform.localScale.y/2;
     if(localPos.y + transform.localScale.y/2 > max.y) localPos.y = max.y - transform.localScale.y/2;
 
     var worldPos = Cam.TransformPoint(localPos);
 
     rigidbody.MovePosition(worldPos);    

     rigidbody.MoveRotation(Cam.rotation);
 }

--David--

Comment
Add comment · 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

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

15 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

Related Questions

Velocity relative to Local Axis 1 Answer

How do I obtain the local velocity of a Rigid body on one axis? 3 Answers

Set The Local Velocity Of A Rigidbody 2 Answers

Setting Local Velocity Of A RigidBody 3 Answers

How do you directly add to velocity in a relative manner? 1 Answer


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