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 stefo66 · May 03, 2014 at 11:38 PM · velocityclamplocalglobal

Clamping Velocity.z when touching an object (Major Edit)

Major edit to reflect new approach to problem:

I am currently in the process of creating a 3d 1st person character. I'm currently having a problem with jumping and obstacles, and am attempting to prevent the character from being able to move forward when he is touching an obstacle (all other directions are ok).

My inital code was as follows:

         if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), 0.6f)) {
             rigidbody.velocity = new Vector3 (
                   rigidbody.velocity.x, 
                   rigidbody.velocity.y, 
                   Mathf.Clamp(rigidbody.velocity.z, -100, 0));
                   }

This worked pretty well, however it only worked in global space, I.E when my character's idea of "forward" was along the positive direction of the z axis. Any other way and it doesn't work.

I then tried:

         if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), 0.6f)) {
              rigidbody.velocity = transform.TransformDirection(
                   rigidbody.velocity.x, 
                   rigidbody.velocity.y, 
                   Mathf.Clamp(rigidbody.velocity.z, -100, 0));
                   }

This doesn't work either, I get some kind of insane push in the negative global Z direction whichever way I am facing, so it only works on obstacles that are in front of me in global space as well as local space. It also makes my x directions swap around.

InverseTransformDirection doesn't work either.

Halp?

Comment
Add comment · Show 1
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 Gruffy · May 04, 2014 at 01:19 PM 0
Share

I think you should check out this bud. It will help you understand how to control the local Positions over the world space positions. In any case, you are only making the call to .localPosition ins$$anonymous$$d of plain old .position It might be best to make sure you check your local position is set correctly firstly by switching to "local" ins$$anonymous$$d of "Global" through the tab button found near the top left of your editor window (the axis controls bit)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · May 04, 2014 at 01:36 PM

In order to make this work, you have to first convert velocity into local space, do the clamp, then convert the velocity back to global space. Something like:

 if (Physics.Raycast (transform.position, transform.forward, 0.6f)) {
       Vector3 localDir = Transform.InverseTransformDirection(rigidbody.velocity);
       localDir.z = Mathf.Clamp(localDir.z, -100, 0);
       rigidbody.velocity = Transform.Tranform.TransformDirection(localDir);
 }
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

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

Related Questions

Local Velocity for Rigidbody? 1 Answer

How do I make rigidbody.velocity in local space? 1 Answer

Global/local axis hotkey 2 Answers

Vertex Coordinates to Global Vector3 2 Answers

Local vs Global Transform 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