Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by mcunha98 · Apr 07, 2020 at 12:22 AM · transform.positionvector3.distance

How limit the movement using distance

How can I limit the distance of my object ? The code work, but the position always is restarted to zero.

             horizontal = horizontal * (movimento * Time.deltaTime);
             vertical = vertical * (movimento * Time.deltaTime);
             Vector3 fwd = new Vector3(horizontal * -1f, 0f, vertical * -1f);
             if (fwd != Vector3.zero)
             {
                 parabola.endPosition.forward = fwd;
             }
 
             Vector3 rot = new Vector3(horizontal, 0f, vertical);
             if (rot != Vector3.zero)
             {
                 parabola.endPosition.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(rot), 10f * Time.deltaTime);
                 parabola.endPosition.Translate(horizontal * -10f, 0f, vertical * -10f, Space.World);
             }
 
             float dist = Vector3.Distance(parabola.startPosition.localPosition, parabola.endPosition.localPosition);
             if (dist > parabola.maxDistance)
             {
                 Vector3 newpos = parabola.endPosition.localPosition - parabola.startPosition.localPosition;
                 newpos = newpos.normalized;
                 newpos *= (dist - parabola.maxDistance);
                 parabola.endPosition.localPosition = newpos;
             }
 

When I change the position of endPosition using parabola.endPosition.localPosition = newpos; the code not work.

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

2 Replies

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

Answer by streeetwalker · Apr 07, 2020 at 05:28 AM

Hi @mcunha98, sorry, but I'm confused. the code you show us. It is not enough to tell what is going on, and you don't explain it - what are you supposed to be moving? The end point of something is supposed to get closer and closer to some fixed point, or what?

To limit the movement of something, here is one way I would do:

 Vector3 moveDist = parabola.endPosition.localPosition - parabola.startPosition.localPosition;
 if ( moveDist.magnitude > parabola.maxDistance ) // moving too far?    
     moveDist  =  moveDist.normalized * parabola.maxDistance; // then limit it
 object.transform.localPosition = object.transform.localPosition + moveDist;

Now, if you want to do something else, let us know!

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

Answer by mcunha98 · Apr 07, 2020 at 12:21 PM

@streeetwalker I'll do a simple test with you shared, but my goal is alt text

I attach the parabola script object to my gameObject (player is the Origin) and a simple Empty is moved by cursor showing the trajectory of the parabola. Its works fine, but the object to finish the trajectory need a limit (max distance) from the origin .

So basically, after move the endPoint using input, I just need to check if I reach the maximum distance and stop to go forward (in my sample code, the step if (dist > parabola.maxDistance) )


parabola.png (87.3 kB)
Comment
Add comment · Show 3 · 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 streeetwalker · Apr 07, 2020 at 03:31 PM 0
Share

@mcunha98, OK, that helps, but still not clear enough.

Is the distance from the origin you want to limit (pick one or describe another option):

1. the motion of an object along the circumference of the parabola?

2. how far from the origin the parabola hits the ground?

3. you want to move an object to the point where the parabola hits the ground?

avatar image mcunha98 · Apr 07, 2020 at 09:37 PM 0
Share

alt text

In the example, I must to limit for example the distance in 9f. 9,094467>9

In code that you suggest, probably the problem is here:

 parabola.endPosition.localPosition = (parabola.endPosition.localPosition + moveDist);


Because the endPosition continue to advanced beyond the limit of 9f

avatar image mcunha98 · Apr 07, 2020 at 09:49 PM 0
Share

Now works fine !!!

Vector3 moveDist = parabola.endPosition.localPosition - parabola.startPosition.localPosition; if (moveDist.magnitude > parabola.maxDistance) { moveDist = moveDist.normalized * parabola.maxDistance; parabola.endPosition.localPosition = moveDist; }

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

198 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

Related Questions

Looking for the optimal smooth incremental movement method. 0 Answers

vector 3 to unity.transform 1 Answer

Ingame Object placing - Place objects like Unturned or Ark Survival 1 Answer

Having problems with setting transform.position 1 Answer

Setting a Global transform.position 0 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