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 DanSparrow · Aug 21, 2017 at 01:25 AM · distancelockrotate-around

Lock object distance to the main object

So, this is something simple and yet haven't been able to find a way to solve it, I'm quite new on this. I want my object to follow the mouse cursor, for which I used this:

     void Update () {
         Vector3 follow = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         follow.z = 5f;
         transform.position = follow;
     }

But while it does, I want this smaller object movement to be restricted around a bigger object (Example: like a moon orbiting a planet) so when you move the mouse, it will simple rotate around this main object.

I mean I can simply do this:

         Vector3 allowedPos = follow - theCenter;
         allowedPos = Vector3.ClampMagnitude(allowedPos, 130.0f);
         transform.position = theCenter+ allowedPos;

but that would restrict the movement to a fixed radius, and I want it to be able to move further or closer to the main object, within a certain range. Is that possible?

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

Answer by SneakyLeprechaun · Aug 21, 2017 at 02:28 AM

Something like this?

public Transform ConstrainToZ; public float Radius; void Update () { Vector3 follow = Camera.main.ScreenToWorldPoint(Input.mousePosition); follow = new Vector3(follow.x - ConstrainToZ.position.x, follow.y - ConstrainToZ.position.y, ConstrainToZ.position.z); follow = follow.normalized * Radius; transform.position = ConstrainToZ.position + follow; }

by subtracting the position of the object you want to rotate around and then normalizing the vector, you get the direction relative to the bigger object. Then by multiplying it by a radius you constrain it to that distance from your desired object.

Comment
Add comment · Show 4 · 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 DanSparrow · Aug 21, 2017 at 02:34 AM 0
Share

Well, that indeed does the trick to have a fixed rotation around the object, but I have a question, you think it is possible to set max and $$anonymous$$ values to the radius, so the smaller object can move closer or further to the main object within that radius?

avatar image SneakyLeprechaun DanSparrow · Aug 21, 2017 at 03:11 AM 1
Share

Yeah, I think so. I haven't tested this, but try it:

ins$$anonymous$$d of setting follow to follow.normalized * Radius, set it it to

follow.normalized * $$anonymous$$athf.Clamp(follow.magnitude,0,Radius);

and it should limit it.

avatar image DanSparrow SneakyLeprechaun · Aug 21, 2017 at 03:14 AM 0
Share

Works like a charm! thank you very much!

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

112 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

Related Questions

Use Quaternion.lookat to rotate On One Axis (for turret.) 1 Answer

help ordering array for planetary gravity 1 Answer

Move player distance forward over time. 0 Answers

Spawn a number of prefabs an equal distance apart 2 Answers

Adding some offset value for RayCast 2D but what is the distance unit? 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