- Home /
Question no longer needs to be answered
I want a border limit from which an object can be moved, c#
Hello,
I am making a game and I have a script where I can move my object around the screen, but I want it so the object can only be moved inside a border. I followed this tutorial on how to make movable objects: https://www.youtube.com/watch?v=hzuxb8CPGyQ
I was thinking using a Trigger 2D Box collider so the object can only move inside that collider, but I'm not sure how to implement that into my script. Here is my script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class UIDrag : MonoBehaviour {
float OffsetX;
float OffsetY;
public void BeginDrag() {
OffsetX = transform.position.x - Input.mousePosition.x;
OffsetY = transform.position.y - Input.mousePosition.y;
}
public void OnMouseDrag()
{
transform.position = new Vector3(OffsetX + Input.mousePosition.x, OffsetY + Input.mousePosition.y);
}
}
I used the EventTrigger component for the script:
Will someone please help me?
Answer by madks13 · Aug 17, 2018 at 08:34 PM
Umm, you could add said collider and do the movement inside OnTriggerStay2D.
For this, you would need a few changes :
Set collider as trigger, don't need collisions with game objects
Inside OnMouseDrag, set a variable to true, so we know we are dragging something
Inside OnTriggerStay2D, if dragging, change element position.
Since the movement is done inside OnTriggerStay2D, the movement will only apply if the drag is inside the collider area.
That said, the collider doesn't collide with mouse cursors, you will need some invisible collidable object following the mouse for the trigger to activate. Best practice would be a ghost element. A preview of the element to move so the user can see where it will be placed.
This is my new script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIDrag : $$anonymous$$onoBehaviour {
float OffsetX;
float OffsetY;
public bool ObjectIs$$anonymous$$oveable;
public void BeginDrag() {
OffsetX = transform.position.x - Input.mousePosition.x;
OffsetY = transform.position.y - Input.mousePosition.y;
}
public void On$$anonymous$$ouseDrag()
{
ObjectIs$$anonymous$$oveable = true;
}
public void OnTriggerStay2D(Collider2D collision)
{
if (ObjectIs$$anonymous$$oveable == true)
{
transform.position = new Vector3(OffsetX + Input.mousePosition.x, OffsetY + Input.mousePosition.y);
}
}
}
Now it's not even movable. The BoxCollider2D is set to Trigger, and the box collider is around the object enough so it's able to move, but it's not moving. I'm confused. @madks13
Err, where did you add the collider on? The collider logic, OnTiggerStay2D, should be on the object containing the collider. But the collider should not be on the object you are dragging, since the collider would move with said object, you would always be inside the collider. For this method to work, you need to have the collider on the parent area, and that parent area will check the boolean inside the draggable object to see if it can be moved and move it if true.
Ok, I did what you said. The gameobject is movable but it's not detecting when the object is outside of the collider, so I used OnTriggerExit2D to set the ObjectIs$$anonymous$$ovable bool to be false, but it didn't disable when I wanted it to. The ObjectIs$$anonymous$$ovable bool does work though, when I manually disable it through the editor, the gameobject stops moving, which is good. So the problem is OnTriggerExit2D isn't working. I have the collider object as the parent of the draggable object. @madks13
Answer by Trevdevs · Aug 17, 2018 at 09:04 PM
Why dont you just clamp the postion ?
public float minX = -10;
public float maxX = 10;
public float minY = -10;
public float maxY = 10;
public void OnMouseDrag()
{
transform.position = new Vector3(OffsetX + Input.mousePosition.x, OffsetY + Input.mousePosition.y);
Vector3 newPosition = transform.position;
newPosition.x = Mathf.Clamp(newPosition.x, minX, maxX);
newPosition.y = Mathf.Clamp(newPosition.y, minY, maxY);
transform.position = newPosition;
}
Since you are using world position, this is not a good idea. It seems to me that the parent elements are movable too, so the clamp would not work in world space. That said, if local space was used, this might be an easier solution.
i've done this using world position it works fine just using children ones make sure it factors in the offset of the child to the parent and you're golden
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Scrollview can't scroll with buttons that have Event Triggers 0 Answers