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 SBull · Aug 21, 2014 at 03:18 AM · javascriptlerpvariables

How would you get one variable to be directly correlated to another variable?

I am trying to figure out a way to cause the distance of ObjectA from it's origin position to directly control the angle of ObjectB. The player controls the position of ObjectA by clicking and dragging with the mouse and I have a hinge joint with max limit controlling the angle of ObjectB.

So for example, the default distance of ObjectA from it's origin is 0, so the angle of ObjectB stays at 0 degrees. But if the player drags ObjectA the max distance away from it's origin (it has a radius of 5), I need the max limit of ObjectB to become 90 degrees. But I also need all of the angles in between to be represented as the player moves through them. So when the player moves ObjectA to half of it's max distance (2.5 units away) the angle of ObjectB will be 45 degrees.

I am not really sure how to search for this cause I'm not sure what this effect would is called. I started looking into Lerp functionality because it seems to be able to represent gradations between two variables, but I'm not sure that it is what I am looking for since from what I have seen, it uses a time variable. Rather than having the value change over time, I need mine to change according to the player's actions. So even if someone doesn't have a direct answer for me, I would appreciate being pointed in the right direction just to get me started on what I need to search for. Thanks in advance for any help!

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

Answer by robertbu · Aug 21, 2014 at 04:33 AM

I believe this is what you are looking for:

   var angle = Mathf.Lerp(minAngle, maxAngle, distance/maxDistance); 
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 SBull · Aug 21, 2014 at 06:57 PM 0
Share

Yes, I think this is what I'm looking for! I didn't know that the third value didn't have to be a measurement of time. However I am still having a bit of trouble implementing it. I have tried a few different things which give me different errors. This is the code I have right now:

 var $$anonymous$$Angle : float = 45;
 var maxAngle : float = -45;
 var distance : Vector3;
 var maxDistance : Vector3;
 var sqrObject : GameObject;
 var launchObject : GameObject;
 var sqrPos : Vector3;
 var launchPos : Vector3;
 var limits = hingeJoint.limits;
 
 function Start()
 {
     sqrObject = gameObject.Find("squirrel");
     launchObject = gameObject.Find("launchObject");
 }
 
 function Update()
 {
     
     sqrPos = sqrObject.transform.position;
     launchPos = launchObject.transform.position;
 
     distance = launchPos - sqrPos;
 
     var angle = $$anonymous$$athf.Lerp($$anonymous$$Angle, maxAngle, distance);
     
     limits.max = angle;
 }

But for some reason this tells me that I can't use $$anonymous$$athf.Lerp with (float,float,float) even though I have distance as a Vector3.

Also I am not quite sure how to declare the maxDist using Vector3s, but when I put (,,distance/maxDistance) in there it tells me that "/" can't be used with a left hand and right hand side of type. I'm not really sure what that means.

avatar image robertbu · Aug 22, 2014 at 01:34 PM 0
Share

You are mixing Vector3s and floats. Given what you have here:

  • $$anonymous$$ake 'maxDistance' a float at the top of the file. Be sure to assign it a value.

  • In the Lerp(), use 'distance.magnitude'.

avatar image SBull · Aug 22, 2014 at 07:05 PM 0
Share

Finally figured out how to put it all together. I was going about a lot of thing very, very wrong before, but it's working perfectly now. I'll post the working code below with comments for other people's future reference. Thanks again robertbu!

 var $$anonymous$$Angle : float;
 var maxAngle : float;
 var sqrObject : Transform;
 var launchObject : Transform;
 
 function Update()
 {
     //Calculates the distance between both objects
     var dist = Vector3.Distance(sqrObject.position, launchObject.position);
 
     //Interpolates angle of object dependant on distance of two objects
     var angle : float = $$anonymous$$athf.Lerp(maxAngle, $$anonymous$$Angle, dist);
     
     //Changes angle of object by limiting hingeJoint max limit property
     hingeJoint.limits.max = angle;
 }
avatar image robertbu · Aug 22, 2014 at 07:19 PM 0
Share

Note your code only works if you define the maximum distance as 1.0. That is any value outside the range of 0 to 1 for Lerp() is clamped to the 0 to 1 range.

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

22 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

Related Questions

Trying to set up a lerp position system for the camera and failing... 2 Answers

How to use Lerp? 3 Answers

How to Lerp or Slerp Time.timeScale change 1 Answer

Rotate towards target doesn't work properly (Quaternion.lerp) 1 Answer

Restricting Movement within a cover system? 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