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 Leroterr · May 29, 2014 at 09:04 AM · androidiosraycasttouchclamp

Limit the position of the object, but still keep following the touch? How to use ClampMagnitude properly?

How do I limit/clamp the position of an object that's following the touch, but still be able to follow the touch?

I want it to be like this one:

alt text

I also tried magnitude.clamp but it just won't work for some reason. Maybe I'm doing something wrong with it? Nothing happens when I insert the magnitude.clamp code. With or without it, the results are the same.

I'm also using a 2nd camera on Orthographic View. Maybe that has something to do with it? The only limiter I'm using right now are the colliders, but it doesn't follow the touch anymore once the touch is outside the collider, and I don't want that to happen.

Here's the code I'm currently using:

 if (Input.touchCount > 0) 
     {        
     Ray ray = camera2.ScreenPointToRay(theTouch[0].position);
     
     //When the touch is out of the "border", this object completely stops following the touch
     //I want it to just limit the position of this object, but still keep following the touch
     //Where it can still detect where my touches are, and then try to follow it, but it can't go outside the border    
     if(Physics.Raycast(ray, out hit) && hit.collider.tag == "border")
         {
             Touch touch = Input.GetTouch(0);
                 
             if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) 
             {
             //Finds the position of the touch    
             touchPosition = camera2.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));          
             
             //Follows the touch
             transform.position = Vector3.Lerp(transform.position, touchPosition, Time.deltaTime * 10);
             }
         }
     }

Thanks! I've tried Google and searching here on Unity Answers but found none unfortunately

Comment
Add comment · Show 6
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 christoph_r · May 29, 2014 at 12:19 PM 1
Share

Can you post the version of your code where you use $$anonymous$$agnitude.Clamp?

avatar image Josh707 · May 29, 2014 at 12:33 PM 1
Share

You should be able to use $$anonymous$$athf.Clamp to limit the axes of the touchPosition vector you are calculating:

 touchPosition = camera2.ScreenToWorldPoint(...); 
 touchPosition.x = $$anonymous$$athf.Clamp(touchPosition.x, -10f, 10f);
 
avatar image Leroterr · May 29, 2014 at 01:18 PM 0
Share

@christoph_r Yeah here's the code, I'm not really sure on how to use it really. if (Input.touchCount > 0) {
Ray ray = camera2.ScreenPointToRay(theTouch[0].position);

         if(Physics.Raycast(ray, out hit) && hit.collider.tag == "border")
            {
              Touch touch = Input.GetTouch(0);
     
              if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.$$anonymous$$oved) 
              {
 
 //THE CLA$$anonymous$$P
 touchPosition = Vector3.Clamp$$anonymous$$agnitude(touchPosition, 10.945309f);  
 
              touchPosition = camera2.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));          
              transform.position = Vector3.Lerp(transform.position, touchPosition, Time.deltaTime * 10);
              }
            }
         }
 
avatar image Leroterr · May 29, 2014 at 01:18 PM 0
Share

@Josh707 Thanks! I'll try it out later when i get home. Can this also be used for $$anonymous$$agnitude.Clamp? I need a circular clamp or something.

avatar image Josh707 · May 29, 2014 at 02:37 PM 1
Share

It would be easier to do something circular without using Clamp methods in my opinion, if you have the center position of the circle you can do some vector subtraction to get a direction.

Something like this:

 public Transform obj; //Object you're dragging around
 public Transform circle_center; //Center of the circle
 float circle_radius = 1f;
     
 //Get the direction/distance towards the object from the center
 Vector3 direction = obj.position - circle_center.position;
     
 /*If the distance is greater than the set radius,
   set 'obj' position to the circle's center with direction
   multiplied by radius added to it*/
 if(direction.magnitude > circle_radius)
     player.position = circle_center.position + (direction.normalized * circle_radius);
Show more comments

1 Reply

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

Answer by rutter · May 29, 2014 at 08:47 PM

Unless you're very sure of what you're doing, you're using Lerp incorrectly.

Assuming 30fps, this Lerp call gives you a point about 33% between pos1 and pos2:

 Vector3 v = Vector3.Lerp(pos1, pos2, Time.deltaTime * 10);

This MoveTowards call gives you a point that moves from pos1 toward pos2 at about 10 meters per second:

 Vector3 v = Vector3.MoveTowards(pos1, pos2, Time.deltaTime * 10);

Finally, I don't see any ClampMagnitude call in your code. If you're trying to clamp within a sphere, that should do the trick. If one axis of the vector is guaranteed to be zero, then it'll clamp on a circle.

This would clamp within 10 units, for example:

 Vector3 v2 = Vector3.ClampMagnitude(v, 10f);
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 Leroterr · May 29, 2014 at 11:03 PM 0
Share

Thanks! But I couldn't get the Clamp$$anonymous$$agnitude to work. Is it because the object isn't in the middle (0,0,0)? I wanted the center of the circle to be on the bottom right of the screen.

I've tried this one and it just stays on middle of the screen when I'm touching it:

 transform.position = Vector3.$$anonymous$$oveTowards(transform.position, touchPosition, Time.deltaTime * 10);
 
 transform.position = Vector3.Clamp$$anonymous$$agnitude(transform.position + centerPos.position, 1f);

Is there something wrong with my script? Thanks :)

avatar image rutter · May 29, 2014 at 11:16 PM 3
Share

If you want a clamped position relative to something other than (0,0,0), you can calculate that:

 //where you want to be (according to $$anonymous$$oveTowards)
 Vector3 desiredPosition = ???

 //your clamp is relative to this position
 Vector3 origin = ???

 //this vector points from 'origin' to 'desiredPosition'
 Vector3 diff = desiredPosition - origin;
 
 //clamp the difference, then add it to the origin
 transform.position = origin + Vector3.Clamp$$anonymous$$agnitude(diff, 1f);

origin is perhaps the position of another object, or a point calculated using ScreenToWorldPoint.

avatar image Leroterr · May 29, 2014 at 11:34 PM 0
Share

Oh wow, thanks! It finally worked with your script.

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

24 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

Related Questions

How to touch select 3D objects 2 Answers

How do you map touch to one of the input axes? 1 Answer

Receiving UI/touch events while mobile soft keyboard is open 0 Answers

getPressure for touch pressure on Android or iOS 3 Answers

How do i create mobile menus 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