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.
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);
}
}
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.
Never$$anonymous$$d, I figured it out thanks
I get an error on line 14 though. Error CS1612 Cannot modify the return value of 'Transform.position' because it is not a variable
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
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.
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.
Thank you it works perfectly, thanks for having patience with me.
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
Follow this Question
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