Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Bobshortsnicker · Feb 12, 2017 at 03:07 PM · x axisdraggable

How do I drag an object along the x axis between two points?

My camera is fixed in my 3D game and my object moves when I drag it but it can go off the screen, I want to set boundaries as to how far it can move. Here is my current code

 {
     bool dragEnabled = false;
     Vector3 dragStartPosition;
     float dragStartDistance;
     void OnMouseDown()
     {
         dragEnabled = true;
         dragStartPosition = transform.position;
         dragStartDistance = (Camera.main.transform.position - transform.position).magnitude;
     }
     void Update()
     {
         if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
         {
             dragEnabled = false;
         }
     }
     void OnMouseDrag()
     {
         if (dragEnabled)
         {
             Vector3 worldDragTo = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, dragStartDistance));
             transform.position = new Vector3(worldDragTo.x, dragStartPosition.y, dragStartPosition.z);
         }
     }
 }

I basically want to set limits to how far my object can be dragged.

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

Answer by oStaiko · Feb 12, 2017 at 04:37 PM

You can just make an editor script to do that. Just throw this on any GameObject and fill in the limits

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 public class ClampPosition : MonoBehaviour
 {
     public Vector2 xValues;
     void Update()
     {
         if (transform.position.x > xValues.y)
             transform.position = new Vector3 (xValues.y, transform.position.y, transform.position.z);
         else if (transform.position.x < xValues.x)
             transform.position = new Vector3 (xValues.x, transform.position.y, transform.position.z);
     }
 }

Comment
Add comment · Show 8 · 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 Bobshortsnicker · Feb 12, 2017 at 04:50 PM 0
Share

Thank you for responding, I am very noob at unity sorry. Where do I fill in the limits? I have made a new editor script, I know how to do that much lol, but I don't know where to type in my x values.

avatar image Bobshortsnicker Bobshortsnicker · Feb 12, 2017 at 04:55 PM 0
Share

Never$$anonymous$$d, I figured it out thanks

avatar image Bobshortsnicker · Feb 12, 2017 at 05:00 PM 0
Share

I get an error on line 14 though. Error CS1612 Cannot modify the return value of 'Transform.position' because it is not a variable

avatar image oStaiko Bobshortsnicker · Feb 12, 2017 at 07:55 PM 0
Share

I have updated it and fixed the error for you. $$anonymous$$ake sure to accept the answer as well so other people can see it

avatar image Bobshortsnicker · Feb 13, 2017 at 12:47 AM 0
Share

I still haven't figured out how to use this, could you help me, this is my first time using unity. The code has no errors now though thanks.

avatar image oStaiko Bobshortsnicker · Feb 13, 2017 at 07:42 AM 0
Share

Alright I have updated the script one more time to make it even simpler! Just save that script as "ClampPosition" in cSharp, attach it to your game object you want to clamp, and set the $$anonymous$$/max x values you want in the inspector. If any step there confuses you, just tell me which part and I'll go more in depth on it.

avatar image Bobshortsnicker oStaiko · Feb 14, 2017 at 02:39 AM 0
Share

Thank you it works perfectly, thanks for having patience with me.

avatar image MontyMomentum · Aug 10, 2020 at 08:06 PM 0
Share

I have tried adjusting this script so the object can only move among the Z axis but it doesn't work, do you happen to have any idea whats wrong?


Here's my script using UnityEngine; using System.Collections; public class clampPosZ : $$anonymous$$onoBehaviour { public Vector2 ZValues; public float dragSpeed = 1f; Vector3 last$$anonymous$$ousePos;

 void On$$anonymous$$ouseDown()
 {
     last$$anonymous$$ousePos = Input.mousePosition;
 }

 void On$$anonymous$$ouseDrag()
 {
     Vector3 delta = Input.mousePosition - last$$anonymous$$ousePos;
     Vector3 pos = transform.position;
     pos.z += delta.z * dragSpeed;
     transform.position = pos;
     last$$anonymous$$ousePos = Input.mousePosition;
 }
 void Update()
 {
     if (transform.position.x > zValues.y)
         transform.position = new Vector3(transform.position.x, transform.position.y, zValues.y);
     else if (transform.position.x < zValues.x)
         transform.position = new Vector3(transform.position.x, transform.position.y, zValues.x);
 }

}

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

94 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

Related Questions

Camera X Rotation Problems 0 Answers

Draggable Door Problems 0 Answers

Need help with coordinate conversion. 1 Answer

Dragging UI elements in Screen Space canvas 1 Answer

Get a constant Speed 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