Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 /
  • Help Room /
avatar image
1
Question by TheFlaxias · May 03, 2021 at 02:45 AM · movementvector3directionclamplimit

ClampMagnitude while ignoring a direction

Hi, I've spent quite some time on this problem while looking at other answers, but I haven't been able to find a solution to this problem. I need to limit movement in 2 direction (transform.forward and transform.right) to a certain radius, while allowing free movement in 1 direction (transform.up). I needed to use transform.up because the movement is not in global up but local up instead. It should be noted that I'm setting the movement using transform.position instead of rigidbody.

My current code is:

 Vector3 offset = updateTransform.position - initPos;
 transform.position = initPos + Vector3.ClampMagnitude(offset, maxDistance);

initPos is the center of the circle


I would greatly appreciate any hints and help to this problem.

Thank you.

Allowing transform.right movement while limiting other direction

clampmagnitudeproblem.jpg (125.6 kB)
Comment
Add comment · Show 3
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 TheFlaxias · May 04, 2021 at 03:20 AM 0
Share

Other things I tried:

 Vector2 localHand = new Vector2(updateTransform.InverseTransformPoint(transform.position).y, updateTransform.InverseTransformPoint(transform.position).z);
 Vector2 localInit = new Vector2(initPos.transform.InverseTransformPoint(transform.position).y, initPos.transform.InverseTransformPoint(transform.position).z);
 Vector2 localOffset = localHand - localInit;
 Vector3 TrueLocalOffset = new Vector3(updateTransform.InverseTransformPoint(transform.position).x - initPos.transform.InverseTransformPoint(transform.position).x, localOffset.x, localOffset.y);
     
 transform.localPosition = initPos.transform.InverseTransformPoint(transform.position) + TrueLocalOffset;

It still didn't work

avatar image LevBronstein · May 05, 2021 at 12:19 AM 0
Share

Can't really figure out, what you're trying to do. Maybe you're trying to make something move like it traveles a vertical tube (bounded by its radius)? In this case you could try clamping the magnitude of your flat (which lies on XZ-plane) 2-D vector with Vector2.ClampMagnitude and then add to this the Y component (the amount your GameObject has traveled along Y axis) as a new 3-D vector.

avatar image TheFlaxias LevBronstein · May 05, 2021 at 01:53 AM 1
Share

Thanks for replying @LevBronstein. You're right, what I needed to do was to make object travel in a vertical tube. But the tube can rotate so I need to do the calculation in the local space. The question has been answered somewhere else. Here's the finished code for anyone else that might need it:

 // Convert global start position to local space
 var localInitPos = transform.InverseTransformPoint(initPos);
 // Only consider position in XZ-space
 var localInitPosXz = new Vector2(localInitPos.y, localInitPos.z);
  
 var localDestPos = transform.InverseTransformPoint(updateTransform.position);
 var localDestPosXz = new Vector2(localDestPos.y, localDestPos.z);
 var offsetXz = localDestPosXz - localInitPosXz;
            
 // Clamp the offset to the radius we want
 var clampedOffsetXz = Vector2.ClampMagnitude(offsetXz, maxRadius);
 // Add the offset to initPos to get the clamped target position
 var clampedPosXz = localInitPosXz + clampedOffsetXz;
 // Convert back to 3D, since we're in local space our current y is just 0
 var localClampedPos = new Vector3(localDestPos.x, clampedPosXz.x, clampedPosXz.y);
 // Apply as global position, applying in local space gets a bit tricky
 transform.position = transform.TransformPoint(localClampedPos);

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

How to SHOOT an object on a curved path without using Rigidbody 2 Answers

My gameobject movement is slightly jumpy... why? 1 Answer

How to disable diagonal Vector3 movement of non-player object using MoveTowards? 1 Answer

Air Control While Jumping? 1 Answer

Animations are not playing with certain directions? 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