Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by Oninji · Dec 08, 2011 at 11:11 PM · rigidbodyvelocityaxiscontrollocal

Velocity relative to Local Axis

Greetings,

Can I add velocity to an object relative to it's local axis?

This is the snippet of code:

 rigidbody.velocity.z = MovSpeed;

But it's relative to the world, I want it to be relative to the local object.

Here is the full script.

 private var lookRotationPoint = Vector3;
 
 public var MovSpeed : float = 10.00;
 
     function Update()
     {        
         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         var hit : RaycastHit;
         if (Physics.Raycast(ray, hit))
         {
             if (hit.collider.gameObject.tag == "Plane")
             {
                 var lookRotationPoint = hit.point - transform.position;
                 transform.rotation = Quaternion.LookRotation(lookRotationPoint.normalized);
                 transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
             }
             
             if(Input.GetMouseButton(1))
             {
                     "Code goes here"
             }
             if(!Input.GetMouseButton(1))
                     {
             rigidbody.velocity = Vector3(0,0,0);
             }
         }    
     }
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
28
Best Answer

Answer by aldonaletto · Dec 08, 2011 at 11:36 PM

Convert the current velocity to local space, modify the z component and convert it back to world space:

   var locVel = transform.InverseTransformDirection(rigidbody.velocity);
   locVel.z = MovSpeed;
   rigidbody.velocity = transform.TransformDirection(locVel);

But if you just want to add/subtract a velocity in the forward direction, use transform.forward:

   rigidbody.velocity += transform.forward * MovSpeed;
Comment
Add comment · Show 7 · 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 Oninji · Dec 09, 2011 at 12:06 AM 0
Share

It works, but halfly. The velocity total doesn't follow the fix value and provoke a slingshot effect.

avatar image aldonaletto · Dec 09, 2011 at 02:34 AM 1
Share

You're modifying only the z local component, what alters the magnitude and the direction; furthermore, transform.forward and rigidbody.velocity may have completely different directions - thus local space (a transform related concept) may not be what you're looking for.
What exactly do you want to do? $$anonymous$$odify the direction without changing the magnitude? Or keep the direction and modify the magnitude? Or none of above?

avatar image Oninji · Dec 10, 2011 at 11:47 PM 0
Share

I want to move the object on a constant pace toward it's own rotation. Thus I want to control velocity through the script and keep it's x,z velocity equal to the $$anonymous$$ovSpeed variable.

I also thought of translating the Y rotation degree into X,Z distribution value (ie 90Y rotation = 0.5x + 0.5z), the distribute the total velocity wanted ($$anonymous$$ovSpeed) to the x and z velocity. But I'm not sure it's the best way to accomplish this.

avatar image aldonaletto · Dec 11, 2011 at 01:40 AM 3
Share

I suppose that this may be what you're looking for:

   rigidbody.velocity = transform.forward * $$anonymous$$ovSpeed;

If you rotate the object, transform.forward follow the direction change, thus this should do what you want. It's advisable to set rigidbody.freezeRotation to true - the object still can be rotated, but physics rotational effects will be disabled, avoiding weird reactions to collisions.

avatar image Oninji · Dec 12, 2011 at 02:04 AM 0
Share

Yes, it behaves exactly how I wanted. Better then my patch-up where I've translated Y rotation to X,Z using Cos and Sin maths.

Erf, and just saw I've used a similar code in an old project, just didn't saw the relation.

Anyway, thank you very much sir.

Show more comments

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

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

velocity direction 1 Answer

Mimic Gravity Movement Via Setting Velocity 0 Answers

rigidbody velocity on x axis and addforce on y axis ?? 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