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 asimov · May 14, 2012 at 11:20 PM · mousepositionclampdragging

Clamping mouse movement when dragging Player object

Hi,

I have a fixed camera facing along the Z-axis and the Player ahead of it. I am using the Unity 'DragRigidbody' script to drag the Player in the X and Y direction (Z is locked). Now, I would like to be able to restrict the amount that a user is able to drag the Player along these axes. The reason for this is the collision detection I have implemented doesn't seem to be reliable when moving the Player quickly/repeatedly against the surrounding walls. The Player still partially/fully moves through them. I am using the 'DontGoThroughThings' script for this.

If anyone can recommend a reliable method of avoiding this issue, it would be greatly appreciated.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by PforPepsi · May 15, 2012 at 02:21 AM

I haven't used this one, but i know that there are different collision checking methods. If you look at the inspector window, there is "Collision Detection" which allows you to chose whether collision checking would be discrete/Continuous/Dynamic Continuous. As far as I remember, Continuous method requires more calculation, i think it uses ray to predict if it is going to hit something.

You can look at it at unity site http://unity3d.com/support/documentation/Components/class-Rigidbody.html

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 asimov · May 17, 2012 at 12:31 PM

Great - thanks for the suggestion PforPepsi.

I have instead decided to clamp the movement as it is more reliable. I will be trying out the discrete/Continuous method very soon, though :)

For other people looking to do something similar, this is what I used:

Note, this must be called from an Update() function as it needs to check/assign the X and Y position of the Player every frame. minX, maxX, minY, and maxY are public float variables at the top of the script so that their values can be tweaked whilst in the Inspector. Simply attach the script to the object you want to clamp the movement of.

     // If Player's X exceeds minX or maxX..
     if (transform.position.x <= minX || transform.position.x >= maxX)
     {
         // Create values between this range (minX to maxX) and store in xPos
         float xPos = Mathf.Clamp(transform.position.x, minX + 0.1f, maxX);

         // Assigns these values to the Transform.position component of the Player
         transform.position = new Vector3(xPos, transform.position.y, 
                                                transform.position.z); 
     }
 
     // If Player's Y exceeds minY or maxY..
     if (transform.position.y <= minY || transform.position.y >= maxY)
     {
         // Create values between this range (minY to maxY) and store in yPos
         float yPos = Mathf.Clamp(transform.position.y, minY, maxY);
          
         // Assigns these values to the Transform.position component of the Player
         transform.position = new Vector3(transform.position.x, yPos, 
                                          transform.position.z);
     }

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
Wiki

Answer by c6y · Nov 29, 2013 at 02:01 PM

Thanks for this. Here's a small edit with the clamp margins:

 // If Player's X exceeds minX or maxX
 if(transform.position.x <= minX || transform.position.x >= maxX){
     
     // Create values between this range (minX to maxX) and store in xPos
     float xPos = Mathf.Clamp(transform.position.x, minX + 0.1f, maxX - 0.1f);
     
     // Assigns these values to the Transform.position component of the Player
     transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
 }
 
 // If Player's Z exceeds minZ or maxZ
 if(transform.position.z <= minZ || transform.position.z >= maxZ){
     float zPos = Mathf.Clamp(transform.position.z, minZ + 0.1f, maxZ - 0.1f);
     transform.position = new Vector3(transform.position.x, transform.position.y, zPos);
 }
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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Drag and Drop lagging behind mouse 0 Answers

2d character, how to limit rotation to z, with character looking left? 0 Answers

How should I Clamp a Mouseposition to a circle? 2 Answers

Mathf.Clamp rotation does not work properly 1 Answer

Line erase Using Line Renderer 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